User
extends Crud
in package
User records: look up accounts for authentication.
Table of Contents
Constants
- RESERVED : mixed = ['me']
- Usernames no account may take, compared case-insensitively: `me` backs the `/user/me` alias for one's own page.
- ROLES : mixed = ['owner', 'admin', 'user']
- The roles a user account may hold.
Properties
- $db : PDO
Methods
- __construct() : mixed
- Open the shared database connection, once.
- findByEmail() : array{id: int, username: string, email: string, hash: string, role: string}|null
- Find a user by email address.
- validate() : array<string, string>
- Validate the fields needed to create a user account.
- read() : array<int, array<string, mixed>>
- Prepare and run a read query, then return its rows.
Constants
RESERVED
Usernames no account may take, compared case-insensitively: `me` backs the `/user/me` alias for one's own page.
public
mixed
RESERVED
= ['me']
ROLES
The roles a user account may hold.
public
mixed
ROLES
= ['owner', 'admin', 'user']
Properties
$db
protected
static PDO
$db
Shared PDO connection, created once on first construction.
Methods
__construct()
Open the shared database connection, once.
public
final __construct() : mixed
The first instantiation builds it from the DB_* constants and the rest
reuse it; a failure triggers E_USER_ERROR. With DEBUG on, statements
are created as DebugStatement so every query execution is timed.
Prepared statements are native, so integer columns come back as int;
the charset is utf8mb4 and the timezone is the server's.
findByEmail()
Find a user by email address.
public
findByEmail(string $email) : array{id: int, username: string, email: string, hash: string, role: string}|null
Parameters
- $email : string
-
Normalised (lower-case) email to look up.
Return values
array{id: int, username: string, email: string, hash: string, role: string}|null —The user row, or null if no user has that email.
validate()
Validate the fields needed to create a user account.
public
static validate(string $email, string $username, string $password, string $role) : array<string, string>
Parameters
- $email : string
-
Email address (identity + login).
- $username : string
-
Public display name.
- $password : string
-
Plain password (checked before hashing).
- $role : string
-
One of
self::ROLES.
Return values
array<string, string> —Error message per invalid field; empty if valid.
read()
Prepare and run a read query, then return its rows.
protected
final read(string $sql[, array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false $param = false ]) : array<int, array<string, mixed>>
Parameters
- $sql : string
-
SQL query with bind placeholders.
- $param : array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false = false
-
Bind parameters (each: [name, value, PDO type, length]), or false for none.
Return values
array<int, array<string, mixed>> —The result rows.