Extended / Inherited
You can extend PDOdb
with your own wrapper class to centralize connection logic,
apply global behaviors (e.g. logging, prefixes, debug flags), or simplify usage in large applications.
Creating your own subclass
To create a custom wrapper around PDOdb
, simply create a child class and pass your config
directly to the parent constructor.
use decMuc\PDOdb\PDOdb;
class MyDatabase extends PDOdb
{
public function __construct()
{
parent::__construct([
'host' => 'localhost',
'username' => 'my_user',
'password' => 'my_pass',
'db' => 'my_project',
'charset' => 'utf8mb4',
'prefix' => 'my_' // optional: applies to all queries in this instance
]);
// Example: set debug level globally
$this->setTrace(true, 'my_');
}
}
This allows you to fully control initialization and behavior from one place. You can also add methods like getUserById()
or project-specific helpers.
Usage example
Using the custom class is now simple:
$db = new MyDatabase();
$users = $db->get('users');
The prefix my_
will automatically be applied to all table names, including joins and subqueries.