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

One common pitfall when using OOP to establish a connection to a database in PHP is not properly handling errors or exceptions that may occur during the connection process. To solve this, make sure to use try-catch blocks to catch any potential errors and handle them accordingly.

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

    public function connect() {
        try {
            $this->conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected successfully";
        } catch(PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }
    }
}

$database = new Database();
$database->connect();
?>