简体   繁体   中英

How will a child class constructor interact with a parent class constructor in php?

Right now, I am developing two classes for interaction with a MySQL database - one extends the other. Here they are:

class DB_MySQL {

    protected $dbuser;
    protected $dbpass;
    protected $dbhost;
    protected $dbname;
    protected $dbh; // Database connection handle

    public function __construct($dbuser, $dbpass, $dbhost, $dbname)
    {
        $this->dbuser = $dbuser;
        $this->dbpass = $$dbpass;
        $this->dbhost = $dbhost;
        $this->dbname = $dbname;
    }

    //Used to create connections - almost always called by execute()
    protected function connect()
    {
        try
        {
            $this->dbh = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
        }
        catch(PDOException $e)
        {
            print "Error!: ".$e->getMessage()."<br/>";
            die();
        }
    }

And the child class:

class CheckOut extends DB_MySQL{

     function __construct() 
    {
        parent::__construct();
    }
}

I just started writing them, so there is no "meat" to them yet. If anyone sees any major issues or any suggestions, don't hesitate to point it out.

My question, though, is how the child constructor will interact with the parent's. My plan is to simply create a CheckOut object without initiating the parent's. As it can be seen, my parent constructor takes four values. So, do I have to redefine those values in the child constructor? To be clear, I mean:

class CheckOut extends DB_MySQL{

     function __construct($dbuser, $dbpass, $dbhost, $dbname) 
    {
        parent::__construct($dbuser, $dbpass, $dbhost, $dbname);
    }
}

and of course define those variables. Or can I add those values when I create the object and it will implicitly be passed?

Any help is appreciated.

If the constructor of the child class doesn't do any other things, then you could omit the constructor.

Just the below is ok:

class CheckOut extends DB_MySQL{
}

But if the child class's constructor need do some other work, yes, you need to do:

class CheckOut extends DB_MySQL{

    public function __construct($dbuser, $dbpass, $dbhost, $dbname) 
    {
        parent::__construct($dbuser, $dbpass, $dbhost, $dbname);
        do_some_other_work();
    }
}

When you use, you both need to call:

$checkout = new CheckOut($dbuser, $dbpass, $dbhost, $dbname);

The code you presented in your question is the correct way to do it in this case (assuming that you want to do something else in the constructor):

class CheckOut extends DB_MySQL{
    function __construct($dbuser, $dbpass, $dbhost, $dbname) 
    {
        parent::__construct($dbuser, $dbpass, $dbhost, $dbname);
    }
}

Otherwise (If you don't want to do anything else in the constructor) you don't need to override the constructor.

class CheckOut extends DB_MySQL{

    // no constructor, Parent's constructor is called by default

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM