'Database Connection Class constructor

I'm just getting started with OOP PHP and all of the information out there is about a car that is red or blue...it's hard to get my head around the database connection only object as such.

I have a haunting suspicion that my __construct() shouldn't have the connection string in it and instead be it's own method inside the class.. but it works so....

Is it incorrect to define my connection class like below.. If it is wrong - how should it look?

class dbConnect {

    // Declare connection info for PDO
    private $dbType = 'mysql';
    private $dbHost = 'localhost';
    private $dbUser = 'user';
    private $dbPass = 'password';
    private $dbName = 'db';
    
    // Declare connection variable for object
    private $dbConn;
    
    // Construct object
    private function __construct() {

        // Create Database connection and assign it to object as dbConn
        $this -> dbConn = new PDO( $this -> dbType . ':' . 'host=' . $this -> dbHost . ';' . 'dbname=' . $this -> dbName , $this -> dbUser , $this -> dbPass );    
    } 
} 


Solution 1:[1]

Yes, you should be extending the existing PDO object.

How about this as your base:

define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_DSN', 'dsn');

class dbConnect extends PDO {           
  public function __construct($user = DB_USER, $pass = DB_PASS, $dsn = DB_DSN) {
    parent::__construct($dsn, $user, $pass, $options);
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin