How can object-oriented programming principles be better applied to the database class in the code snippet?

The object-oriented programming principles can be better applied to the database class by encapsulating the database connection details within the class, using private properties for the connection details, and creating methods within the class to interact with the database. This will make the database class more modular, reusable, and easier to maintain.

class Database {
    private $host = 'localhost';
    private $username = 'root';
    private $password = '';
    private $dbname = 'my_database';
    private $conn;

    public function __construct() {
        $this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname);
        if ($this->conn->connect_error) {
            die("Connection failed: " . $this->conn->connect_error);
        }
    }

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

    public function fetch($result) {
        return $result->fetch_assoc();
    }

    public function close() {
        $this->conn->close();
    }
}

// Example usage
$db = new Database();
$result = $db->query("SELECT * FROM users");
while($row = $db->fetch($result)) {
    echo $row['username'] . "<br>";
}
$db->close();