Database
in package
Central database connection manager for PHPEz.
Provides a static, singleton PDO connection with lazy initialization. Configuration is set once via cfg(), then get() retrieves the connection. All database operations use this connection.
Features:
- Lazy connection initialization (connects on first use)
- Persistent connections (PDO::ATTR_PERSISTENT)
- Automatic exception mode for errors
- Associative array fetch mode
- Table name prefixing support
Usage:
Database::cfg(
'mysql:host=localhost;dbname=myapp',
'user',
'password',
'app_' // optional prefix
);
$pdo = Database::get();
Tags
Table of Contents
Properties
- $connection : PDO|null
- The singleton PDO connection instance.
- $dsn : string|null
- Configured DSN (Data Source Name).
- $pass : string|null
- Configured database password.
- $pfx : string
- Table name prefix for all models.
- $user : string|null
- Configured database username.
Methods
- cfg() : void
- Configure the database connection parameters.
- connect() : void
- Establish a PDO connection if not already connected.
- get() : PDO
- Get the singleton PDO connection instance.
- pfx() : string
- Get the configured table name prefix.
Properties
$connection
The singleton PDO connection instance.
private
static PDO|null
$connection
= \null
$dsn
Configured DSN (Data Source Name).
private
static string|null
$dsn
= \null
$pass
Configured database password.
private
static string|null
$pass
= \null
$pfx
Table name prefix for all models.
private
static string
$pfx
= ''
$user
Configured database username.
private
static string|null
$user
= \null
Methods
cfg()
Configure the database connection parameters.
public
static cfg(string $dsn, string $user, string $pass[, string $pfx = '' ]) : void
Must be called before any database operations. Configuration is stored statically and reused for all future connections.
Parameters
- $dsn : string
-
PDO Data Source Name (e.g., 'mysql:host=localhost;dbname=mydb').
- $user : string
-
Database username.
- $pass : string
-
Database password.
- $pfx : string = ''
-
Optional table name prefix (appended to all table names).
connect()
Establish a PDO connection if not already connected.
public
static connect() : void
Called automatically by get(). Throws DataException if cfg() was not called first.
Tags
get()
Get the singleton PDO connection instance.
public
static get() : PDO
Lazily initializes the connection on first call. Subsequent calls return the same connection instance.
Return values
PDO —The database connection.
pfx()
Get the configured table name prefix.
public
static pfx() : string
Return values
string —The prefix set during cfg(), or empty string if not set.