Oihana PHP System

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
example

Basic usage:

use oihana\db\mysql\MysqlPDOBuilder;

$pdoBuilder = new MysqlPDOBuilder([
    'host'     => 'localhost',
    'dbname'   => 'test_db',
    'username' => 'root',
    'password' => 'secret',
]);

$pdo = $pdoBuilder(); // Returns a configured PDO instance

Skipping database validation:

$pdoBuilder = new MysqlPDOBuilder([
    'host'         => 'localhost',
    'username'     => 'admin',
    'skipDbName'   => true,
    'validate'     => false,
]);

$pdo = $pdoBuilder(); // No validation performed
author

Marc Alcaraz (ekameleon)

since
1.0.0

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]

Properties

$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
example
$builder = new MysqlPDOBuilder([
    'host'     => '127.0.0.1',
    'dbname'   => 'shop',
    'username' => 'admin',
    'password' => '1234',
]);

__invoke()

Creates and returns a new PDO instance.

public __invoke() : PDO|null
Tags
throws
InvalidArgumentException

If validation fails.

example
$builder = new MysqlPDOBuilder
([
    'host'     => 'localhost',
    'dbname'   => 'demo',
    'username' => 'root',
    'password' => 'root',
]);

$pdo = $builder();
Return values
PDO|null

The PDO connection, or null if validation fails.

__toString()

Returns a String representation of the object.

public __toString() : string
Tags
throws
ReflectionException
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
example
$builder = new MysqlPDOBuilder();

$builder->set
([
     'host'     => '127.0.0.1',
     'dbname'   => 'shop',
     'username' => 'admin',
     'password' => '1234',
 ]);

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
throws
InvalidArgumentException

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>
Return values
PDO

        
On this page

Search results