Luminova Framework

PHP Luminova: Native PDO and MySQli Database Driver Connection

Last updated: 2026-11-28 20:57:12

Luminova’s database connection wrapper for accessing and managing native PDO or MySQLi connections.

The DriverConnection class is a thin wrapper around the native database connection used by a Luminova driver. It gives you one object for both PDO and MySQLi, that allows you to check which one is in use, and forwards any other method call to the native PDO or mysqli object.

Use it when you need driver specific features that the PDO and MySQLi driver APIs don't cover.

Getting Started

Pass an existing PDO or MySQLi connection to DriverConnection to access it through a common wrapper.

use Luminova\Database\Driver\DriverConnection;

$pdo = new PDO('sqlite::memory:');
$wrapper = new DriverConnection($pdo);

Accessing the Native Connection

Call getConn() on the wrapper to get the native object, then check its type before using driver specific methods:

$native = $wrapper->getConn(); // PDO, mysqli, or null

if ($wrapper->isPdo()) {
    $stmt = $native->query('SELECT * FROM users');
    $users = $stmt->fetchAll();
} elseif ($wrapper->isMysqli()) {
    $result = $native->query('SELECT * FROM users');
    $users = $result->fetch_all(MYSQLI_ASSOC);
}

Proxied Method Calls

Any method that the wrapper itself doesn't define is forwarded to the native object, so you can call it directly on the wrapper:

$wrapper->query('SELECT 1');           // PDO::query() or mysqli::query()
$wrapper->beginTransaction();          // PDO only
$wrapper->real_escape_string('text');  // mysqli only

The wrapper defines close(), getConn(), isConnected(), isMysqli() and isPdo() itself, so those are never forwarded.

Additional Notes

  • Closing through the wrapper:close() on the wrapper only affects the wrapper and the native connection. The driver keeps its own reference and connection state, so use the driver's close() or Connection::disconnect() to close a connection the framework manages. See close.

  • No connection:Calling a proxied method when the wrapper holds no connection throws BadMethodCallException. Check isConnected() first.


Class Definition


Methods

__construct

Creates the wrapper around a native connection.

public function __construct(PDO|mysqli|null $conn = null)

Parameters:

ParameterTypeDescription
$connPDO|mysqli|nullThe native connection object (default null).

getConn

Gets the native connection object.

public function getConn(): PDO|mysqli|null

Return:

PDO|mysqli|null - Return the native connection, or null if the wrapper was closed or has no connection.

Example:

$native = $wrapper->getConn();

isPdo

Checks whether the native connection is a PDO instance.

public function isPdo(): bool

Return:

bool - Returns true if the connection is a PDO instance, or false otherwise.


isMysqli

Checks whether the native connection is a mysqli instance.

public function isMysqli(): bool

Return:

bool - Returns true if the connection is a MySQLi instance, or false otherwise.


isConnected

Checks whether the connection can run queries. It executes SELECT 1 on the native connection, so it makes a round trip to the server on every call.

public function isConnected(): bool

Return:

bool - Return true if the query runs, or false if there is no connection or the query fails.

The driver's own isConnected() only checks internal state and does not query the server.


close

Closes the native connection held by the wrapper. For mysqli, mysqli::close() is called. For PDO, the wrapper releases its reference, and the connection closes once nothing else references it.

public function close(): bool

Return:

bool - Returns true if connection is close, otherwise false.

The driver still holds its own reference to the native object, so closing a PDO connection through the wrapper does not close it while the driver is alive. A mysqli connection closed through the wrapper is closed for the driver as well, but the driver's isConnected() keeps returning true. Use $db->close() on the driver or Connection::disconnect() to close the connection and update the driver state.


Proxy Methods

The proxy methods provide access to methods and properties of the underlying PDO or MySQLi connection object when they are not directly handled by the wrapper.

  • __call() forwards calls to methods not defined by the wrapper.
  • __get() forwards property read access to the underlying connection.
  • __isset() checks whether a property exists and is set on the underlying connection.

PHP invokes these magic methods automatically when the corresponding operation is performed on the wrapper.

Note:

Accessing or modifying the native connection directly bypasses the driver's internal handling. As a result, statement state, savepoint tracking, and query profiling may not be updated.


__call

Proxies method calls to the underlying PDO or MySQLi connection.

public function __call(string $method, array $arguments): mixed

Parameters:

ParameterTypeDescription
$methodstringThe native method name.
$argumentsarray<array-key,mixed>The arguments passed to the native method.

Return:

mixed - Return whatever the native method returns.

Throws:

  • BadMethodCallException - If the connection is unavailable or the method does not exist.
  • Throwable - If the underlying method throws an exception.

Example:

if ($wrapper->isPdo()) {
    $wrapper->beginTransaction();
    $wrapper->exec("UPDATE users SET status = 'active' WHERE id = 1");
    $wrapper->commit();
}

__get

Retrieves a property from the underlying database connection.

public function __get(string $property): mixed

Parameters:

ParameterTypeDescription
$propertystringThe property name.

Return:

mixed - Returns the property value, or null if the connection is unavailable or the property does not exist.


__isset

Checks whether a property exists on the underlying database connection.

public function __isset(string $property): bool

Parameters:

ParameterTypeDescription
$propertystringThe property name.

Return:

bool - Returns true if the property exists, or false otherwise.