Standard Initialization

Simple PDOdb initialization

To initialize the default PDOdb instance manually, simply pass your database configuration into the constructor. This creates a fully usable, independent connection.

require 'vendor/autoload.php';

use decMuc\PDOdb\PDOdb;

$db = new PDOdb([
    'host'     => 'localhost',
    'username' => 'your_user',
    'password' => 'your_password',
    'db'       => 'your_database',
    'charset'  => 'utf8mb4'
    // 'prefix' => 'wp_' // optional: sets a global prefix for all table names in this instance
]);

// Alternatively set prefix after initialization:
// $db->setPrefix('wp_'); // overrides any prefix passed in the constructor

If a prefix is defined (either in the constructor or later via setPrefix()), it will be automatically applied to all table names used in queries, joins, and subqueries within this instance.

If you call setPrefix() after initialization, it will override any prefix previously defined in the constructor.

This approach creates a fresh instance and does not rely on the internal instance manager. If you want to create multiple named instances or access the default instance globally, see the page Multiple Instances.

Accessing the default instance globally

Once initialized, you can access the default instance from anywhere in your application using the static method PDOdb::getInstance().

// Example inside a function
function getUserList() {
    $db = PDOdb::getInstance();
    return $db->get('users');
}

// Example in a class property
use decMuc\PDOdb\PDOdb;

class UserService {
    protected PDOdb $db;

    public function __construct() {
        $this->db = PDOdb::getInstance();
    }
}

This works only if the default instance has already been initialized beforehand. To use multiple separate connections, see Multiple Instances.