Oihana PHP System

MysqlDSN

Represents a MySQL DSN (Data Source Name) configuration object.

This class provides a convenient and structured way to build MySQL DSN strings used in PDO connections. It supports common connection parameters such as host, port, database name, charset, and optional Unix socket path.

Tags
example
use oihana\db\mysql\MysqlDSN;

$dsn = new MysqlDSN
([
    MysqlDSN::HOST        => '127.0.0.1',
    MysqlDSN::PORT        => 3306,
    MysqlDSN::DBNAME      => 'my_database',
    MysqlDSN::CHARSET     => 'utf8mb4',
    MysqlDSN::UNIX_SOCKET => '/tmp/mysql.sock',
]);

echo (string) $dsn;
// Output: mysql:host=127.0.0.1;port=3306;dbname=my_database;charset=utf8mb4;unix_socket=/tmp/mysql.sock
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

CHARSET  = 'charset'
DBNAME  = 'dbname'
HOST  = 'host'
PORT  = 'port'
PREFIX  = 'mysql:'
UNIX_SOCKET  = 'unixSocket'

Properties

$charset  : string
Character set used for the connection.
$dbname  : string|null
Name of the database to connect to.
$host  : string
Hostname or IP address of the MySQL server.
$port  : int
Port number on which the MySQL server is listening.
$unixSocket  : string|null
Optional Unix socket path for local MySQL connections.

Methods

__construct()  : mixed
Constructs a new MysqlDSN instance.
__toString()  : string
Builds and returns the DSN string for use with PDO.
toArray()  : array<string, mixed>
Converts the DSN configuration to an associative array.

Constants

CHARSET

public mixed CHARSET = 'charset'

UNIX_SOCKET

public mixed UNIX_SOCKET = 'unixSocket'

Properties

$charset

Character set used for the connection.

public string $charset

$dbname

Name of the database to connect to.

public string|null $dbname

$host

Hostname or IP address of the MySQL server.

public string $host

$port

Port number on which the MySQL server is listening.

public int $port

$unixSocket

Optional Unix socket path for local MySQL connections.

public string|null $unixSocket

Methods

__construct()

Constructs a new MysqlDSN instance.

public __construct([array{charset: string|null, dbname: string|null, host: string|null, port: string|null, unixSocket: string|null} $init = [] ]) : mixed
Parameters
$init : array{charset: string|null, dbname: string|null, host: string|null, port: string|null, unixSocket: string|null} = []

An associative array of parameters to initialize the DSN.

Tags
example
$dsn = new MysqlDSN
([
    MysqlDSN::DBNAME => 'example_db',
    MysqlDSN::HOST   => 'localhost',
]);

__toString()

Builds and returns the DSN string for use with PDO.

public __toString() : string
Tags
example
$dsn = new MysqlDSN
([
    MysqlDSN::DBNAME  => 'test',
    MysqlDSN::HOST    => 'localhost',
    MysqlDSN::PORT    => 3307,
]);

echo (string) $dsn ;
// mysql:host=localhost;port=3307;dbname=test;charset=utf8mb4
Return values
string

The DSN string.

toArray()

Converts the DSN configuration to an associative array.

public toArray() : array<string, mixed>
Tags
example
$dsn = new MysqlDSN([ MysqlDSN::DBNAME => 'test' ]);
print_r( $dsn->toArray() ) ;
Return values
array<string, mixed>

The configuration as an associative array.


        
On this page

Search results