PHP PDO MySql

Hello, I’m not very good at PHP(OOP) but I can’t figure out why I get this error

Fatal error: Call to a member function query() on a non-object in

Here is the code, but when I make this query in the db class, there is no error, the selection passes but the inherited class does not work, please explain what I am doing wrong, thank you very much

class db {
    const DB_HOST = "localhost";    
    const DB_NAME = "test";    
    const DB_USER = "root";    
    const DB_PASS = "";
    protected $DB;
    function __construct() {
        $DB = new PDO('mysql:host='.self::DB_HOST.';dbname='.self::DB_NAME.';charset=UTF-8', self::DB_USER, self::DB_PASS);
    }
    function __destruct() {
        unset($DB);
    }
}
class web extends db {
    function viewWeb() {
        $STH = $this->DB->query('SELECT web_id, web_name, web_img FROM web');  
        $STH->setFetchMode(PDO::FETCH_OBJ);
        while($row = $STH->fetch()) {  
            echo $row->web_id . "\n";  
            echo $row->web_name . "\n";  
            echo $row->web_img . "\n";  
        }
    }    
}
$n = new web();
$n->viewWeb();

Answer 1, authority 100%

function __construct() {
    $this->DB = new PDO('mysql:host='.self::DB_HOST.';dbname='.self::DB_NAME.';charset=UTF-8', self::DB_USER, self::DB_PASS);
}
function __destruct() {
    unset($this->DB);
}

Don’t forget $this=)


Answer 2

That’s right.

To avoid the error, change the web method to something else. Otherwise, it overrides the parent constructor.