PHP Luminova: Native PDO and MySQli Database Driver Connection
Learn how to access and manage native PDO and MySQLi database connections through Luminova's database driver connection wrapper.
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
You don't create this class yourself. Get it from the driver, or from a Connection instance:
use Luminova\Database\Connection;
$conn = Connection::getInstance();
// From the Connection class
$wrapper = $conn->getDriverConn();
// From the driver
$wrapper = $conn->database()->getConn();Connection::getDriverConn() returns null when no driver is available. getConn() on a driver always returns a wrapper.
Accessing the Native Connection
Call getConn() on the wrapper to get the native object, then check its type before using driver specific methods:
$wrapper = $conn->getDriverConn();
$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 onlyThe wrapper defines close(), getConn(), isConnected(), isMysqli() and isPdo() itself, so those are never forwarded.
Additional Notes
Each call returns a same wrapper:
getConn()on a driver returnsDriverConnectiontied to same object, point to the same native connection.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'sclose()orConnection::disconnect()to close a connection the framework manages. See close.No connection:Calling a proxied method when the wrapper holds no connection raises a PHP
Error. CheckisConnected()first.
Class Definition
- Class namespace:
Luminova\Database\Driver\DriverConnection - This class is
final - This class implements: \Luminova\Interface\DriverConnectionInterface
Methods
__construct
Creates the wrapper around a native connection. The framework calls it from the driver's getConn().
public __construct(PDO|mysqli|null $conn = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$conn | PDO|mysqli|null | The native connection object. |
getConn
Gets the native connection object.
public getConn(): PDO|mysqli|nullReturn Value:
PDO|mysqli|null - Return the native connection, or null if the wrapper was closed or has no connection.
Example:
$native = $conn->database()->getConn()->getConn();isPdo
Checks whether the native connection is a PDO instance.
public isPdo(): boolisMysqli
Checks whether the native connection is a mysqli instance.
public isMysqli(): boolisConnected
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 isConnected(): boolReturn Value:
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 close(): voidThe driver still holds its own reference to the native object, so closing a
PDOconnection through the wrapper does not close it while the driver is alive. Amysqliconnection closed through the wrapper is closed for the driver as well, but the driver'sisConnected()keeps returningtrue. Use$db->close()on the driver orConnection::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 __call(string $method, array $arguments): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | The native method name. |
$arguments | array | The arguments passed to the native method. |
Return Value:
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:
$wrapper = $conn->getDriverConn();
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 __get(string $property): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$property | string | The property name. |
Return Value:
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 __isset(string $property): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$property | string | The property name. |
Return Value:
bool - Returns true if the property exists, or false otherwise.