How can the Database class be extended or modified to accommodate the use of mysqli or PDO in PHP?

To accommodate the use of mysqli or PDO in PHP, the Database class can be extended to include methods that utilize these database extensions instead of the traditional mysql functions. This can be achieved by creating separate classes for mysqli and PDO connections, and then modifying the Database class to use these classes based on the chosen database extension. By implementing this approach, the Database class can seamlessly switch between mysqli and PDO without affecting the rest of the application.

class MysqliConnection {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }

    // Add more methods as needed
}

class PDOConnection {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $dsn = "mysql:host=$host;dbname=$database";
        $this->connection = new PDO($dsn, $username, $password);
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }

    // Add more methods as needed
}

class Database {
    private $connection;

    public function __construct($host, $username, $password, $database, $type = 'mysqli') {
        if ($type == 'mysqli') {
            $this->connection = new MysqliConnection($host, $username, $password, $database);
        } elseif ($type == 'pdo') {
            $this->connection = new PDOConnection($host, $username, $password, $database);
        }
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }

    // Add more methods as needed
}