MysqlPDOBuilder uses ToStringTrait
Builds and configures a PDO connection to a MySQL database.
This class simplifies the creation of a configured PDO
instance by wrapping
the DSN, authentication credentials, options, and validation logic.
Tags
Table of Contents
Constants
- DEFAULT_OPTIONS = [\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => true, \PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_STRINGIFY_FETCHES => false]
- OPTIONS = 'options'
- PASSWORD = 'password'
- SKIP_DB_NAME = 'skipDbName'
- USERNAME = 'username'
- VALIDATE = 'validate'
Properties
- $dsn : MysqlDSN
- The DSN configuration object.
- $options : array<string|int, mixed>
- Additional PDO options.
- $password : string|null
- The user password for connection.
- $skipDbName : bool
- Whether to skip validation of the database name.
- $username : string|null
- Username used for the PDO connection.
- $validate : bool
- Whether to perform validation before establishing connection.
Methods
- __construct() : mixed
- Initializes the builder with given configuration.
- __invoke() : PDO|null
- Creates and returns a new PDO instance.
- __toString() : string
- Returns a String representation of the object.
- set() : void
- Sets the builder with given configuration.
- toArray() : array<string, mixed>
- Returns the full configuration as array (dsn + credentials + options).
- validate() : void
- Validates required DSN fields.
- createPDO() : PDO
- The internal PDO maker method.
Constants
DEFAULT_OPTIONS
public
mixed
DEFAULT_OPTIONS
= [\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => true, \PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_STRINGIFY_FETCHES => false]
OPTIONS
public
mixed
OPTIONS
= 'options'
PASSWORD
public
mixed
PASSWORD
= 'password'
SKIP_DB_NAME
public
mixed
SKIP_DB_NAME
= 'skipDbName'
USERNAME
public
mixed
USERNAME
= 'username'
VALIDATE
public
mixed
VALIDATE
= 'validate'
Properties
$dsn
The DSN configuration object.
public
MysqlDSN
$dsn
$options
Additional PDO options.
public
array<string|int, mixed>
$options
= []
$password
The user password for connection.
public
string|null
$password
$skipDbName
Whether to skip validation of the database name.
public
bool
$skipDbName
= false
$username
Username used for the PDO connection.
public
string|null
$username
$validate
Whether to perform validation before establishing connection.
public
bool
$validate
= true
Methods
__construct()
Initializes the builder with given configuration.
public
__construct([array{charset: string|null, dbname: string|null, host: string|null, options: array|null, password: string|null, port: string|null, skipDbName: bool|null, unixSocket: string|null, username: string|null, validate: bool|null} $init = [] ]) : mixed
Parameters
- $init : array{charset: string|null, dbname: string|null, host: string|null, options: array|null, password: string|null, port: string|null, skipDbName: bool|null, unixSocket: string|null, username: string|null, validate: bool|null} = []
-
Associative array of connection parameters:
host
,port
,dbname
,charset
,unixSocket
(for DSN)username
,password
(for credentials)options
(PDO options)skipDbName
(bool)validate
(bool)
Tags
__invoke()
Creates and returns a new PDO instance.
public
__invoke() : PDO|null
Tags
Return values
PDO|null —The PDO connection, or null if validation fails.
__toString()
Returns a String representation of the object.
public
__toString() : string
Tags
Return values
string —A string representation of the object.
set()
Sets the builder with given configuration.
public
set([array{charset: string|null, dbname: string|null, host: string|null, options: array|null, password: string|null, port: string|null, skipDbName: bool|null, unixSocket: string|null, username: string|null, validate: bool|null} $init = [] ]) : void
Parameters
- $init : array{charset: string|null, dbname: string|null, host: string|null, options: array|null, password: string|null, port: string|null, skipDbName: bool|null, unixSocket: string|null, username: string|null, validate: bool|null} = []
-
Associative array of connection parameters:
host
,port
,dbname
,charset
,unixSocket
(for DSN)username
,password
(for credentials)options
(PDO options)skipDbName
(bool)validate
(bool)
Tags
toArray()
Returns the full configuration as array (dsn + credentials + options).
public
toArray() : array<string, mixed>
Return values
array<string, mixed>validate()
Validates required DSN fields.
public
validate() : void
Tags
createPDO()
The internal PDO maker method.
protected
createPDO(string $dsn, string|null $user, string|null $pass, array<string|int, mixed> $options) : PDO
Parameters
- $dsn : string
- $user : string|null
- $pass : string|null
- $options : array<string|int, mixed>