Home » Php » Override parent's parent's constructor in PHP

Override parent's parent's constructor in PHP

Posted by: admin July 12, 2020 Leave a comment


I have one PHP class thus:

class DB extends mysqli{
    public function __construct(

My problem is that I want to override this class with a new one that performs more privileged database operations with a different db user.

class adminDB extends DB{
    public function __construct(

What should I do here?

How to&Answers:

You should pass the credentials to the constructor anyway:

class DB extends mysqli {
    public function __construct($host, $user, $password, $db)
        parent::__construct($host, $user, $password, $db);

Then you don’t need inheritance you can just use:

$adminDb = new DB($adminHost, $adminUser, $adminPassword, $db);
$nonAdminDb = new DB($host, $user, $password, $db);

But if you really want inheritance you could still do this:

class AdminDB extends DB {
    public function __construct() {


    class mohona{
        public $name;
        public $age;
        public $fname;
        public $lname;
        public function __construct($cname,$cage,$cfname,$clname){

        public function getMohona(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>";
    class ibrahim extends mohona{
        public $relational_status;
        public $relation;
        public $contact;
        public function __construct($cname,$cage,$cfname,$clname,$crelational_status,$crelation,$ccontact){
        public function getIbrahim(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>"."Relational Status: ".$this->relational_status."<br/>Maritual Status: ".$this->relation."<br/>Contact Status: ".$this->contact;
    $oMohona=new mohona("Mohona","20","Nafis","Anjum");
    $oIbrahim=new ibrahim("Ibu","25","Ibrahim","Akbar","Single","Unmarried","blocked");
    echo $oMohona->getMohona();
    echo $oIbrahim->getIbrahim();