Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP not automatically call parent constructors?

Fairly straightforward question. In C++ the parent constructor will be implicitly called before the child constructor, so what logic is there for PHP not to do things this way?

EDIT: I've got a good answer from Lukman, but I was hoping for more of a reason why there is a difference. Maybe the question should be why does C++ not allow custom calling of parent constructors? I guess that's another question though.

like image 893
Skilldrick Avatar asked Dec 15 '09 10:12

Skilldrick


People also ask

Why does PHP not automatically call parent constructor?

Define a constructor in the child classWhen a child class has its own constructor, the constructor in the child class will not call the constructor of its parent class automatically. To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments) syntax.

How can we call the parent constructor in PHP?

In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

How can we call a parent class constructor?

And the child objects can extend the parent properties. For calling the constructor of a parent class we can use the super keyword. The super() method from the constructor method is used for the invocation of the constructor method of the parent class to get access to the parent's properties and methods.

How do you call a constructor in PHP?

A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!


2 Answers

I think it's a good thing that PHP makes you call parent's constructor manually, because it allows child's constructor such as following:

public function __construct() {    // set up variables that parent::__construct() requires    $var1 = get_stuff_from_db();    $var2 = get_stuff_from_webservice();     parent::__construct($var1, $var2);     // continue setting up $this var    $this->default = 'Default';    $this->do_some_secret_stuff(); } 

Or even:

public function __construct($param) {    // call differently based on condition    if (is_array($param))       $param['id'] = 0;       parent::__construct($param);    }    else {       parent::__construct($param, 0, TRUE);    }     // continue setting up $this var    $this->default = 'Default';    $this->do_some_secret_stuff(); } 

Meaning, you are free to call the parent constructor anywhere within the child's and you are free to do stuff before and after the call. Ain't that a feature indeed?

like image 124
Lukman Avatar answered Oct 13 '22 20:10

Lukman


When you don't have a constructor in the child class, then the parent one is automatically called.

If you decided to add a constructor to the child class, then of course you need to explicitly call the parent constructor. If you are already taking the time to add a constructor to your child class, then adding one more line of code parent::__construct(); doesn't seem to be a big deal. But the overriding is actually a convenient flexibility.

like image 22
prograhammer Avatar answered Oct 13 '22 21:10

prograhammer