What is the best practice for using a mysqli object in a constructor of a class in PHP?

When using a mysqli object in a constructor of a class in PHP, it is best practice to pass the mysqli object as a parameter to the constructor rather than creating a new mysqli object within the constructor. This allows for better flexibility, reusability, and separation of concerns in your code.

class Database {
    private $mysqli;

    public function __construct($mysqli) {
        $this->mysqli = $mysqli;
    }

    public function query($sql) {
        // Use $this->mysqli to execute the query
    }
}

// Usage
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
$database = new Database($mysqli);
$database->query('SELECT * FROM table');