What are common pitfalls when using OOP to establish a database connection in PHP?

One common pitfall when using OOP to establish a database connection in PHP is not handling connection errors properly. To solve this issue, always include error handling to catch any potential connection errors and handle them appropriately.

<?php
class Database {
    private $host = 'localhost';
    private $username = 'root';
    private $password = '';
    private $database = 'my_database';
    private $conn;

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

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

// Example usage
$db = new Database();
$conn = $db->getConnection();
?>