Walaikum Salam.
It seems, you have removed extension from the second class. Now Second class look ok.
Which one you will use depends on your approach.
But second class has a variable $db; which is not private. Can be modified out side of the class, and at the beginning of the class you are checking whether $this->db exists or not; in construction $this->db does not exists anyway;
<?php
class cc {
public function __construct() {
$this->a = 10;
$this->b = 20;
}
}
$x= new cc();
print_r($x);
Result:
cc Object
(
[a] => 10
[b] => 20
)
Which mean it works; but not a good practice. Will lead to unexpected disaster.
Your first class,
$db = new DB();
$res = $db->query("The Query");
This has great advantage. It will let you directly access any function of PDO Class without any restriction. This is useful, when working as team. You still can add functions to do additional job. And you can wrap existing function of PDO if you have to.
On the other hand, with second class, you will have to implement many functions to access individual function, this is going to be problematic in the long run.
So, I will still suggest you to use the first class.
About the question you have asked related to set attribute:
class DB extends PDO {
...
...
}
$conn = new DB();
conn->setAttribute(PDO::CONSTANT, PDO::CONSTANT); <-- this will solve your attribute setting issue. Please, take your time to understand how class, extended class works.
$conn->setAttribute(DB::CONSTANT, DB::CONSTANT);
You should not open more than one connection in a single http request, you need to find a way to let the connection float around in the entire request and make sure that connection closed at the end of request.