I was wondering why we have to explicitly call parent constructor in PHP? What happen if, let's say, we forget to call the parent constructor in the derived class?
I thought that the derived class won't have access to the member properties or method of its parent class but after I try not to call parent constructor, it seems that the child class still get access to its parent public/protected member.
So do we have to explicitly call its parent constructor? What are the consequences if we forget to do so?
Thanks in advance. Any kinds of help will be appreciated :)
The fact that PHP always calls the "nearest" constructor, that is if there is no child constructor it will call the parent constructor and not the grandparent constructor, means that we need to call the parent constructor ourselves. We can do this by using the special function call parent::__construct().
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. If we define Parent class constructor inside Child class it will give compile time error for return type and consider it a method.
If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.
It depends on your code. In it's simpliest nothing happens if you dont call parent constructor, but if your parent constructor does something initializing for parent class, all that is left out. Consider, and play with this:
<?php
class X {
private $_a;
public function __construct($a) {
echo 'X::construct ';
$this->_a = $a;
}
public function getA() {
return $this->_a;
}
}
class Y extends X {
private $_b;
public function __construct($a,$b) {
echo 'Y::construct ';
parent::__construct($a);
$this->_b = $b;
}
public function getAB() {
return $this->_b + $this->getA();
}
}
$n = new Y(3,2);
echo $n->getAB();
It's easy to see that if you run this, it prints out 5 (among other dump). But if you comment class Y
parent call parent::__construct($a);
, you get 2. That is the consequence in this case.
Because parent constructors are not called implicitly if the child class defines a constructor. If the child class assumes that the parent has initialized and done some work, it needs to ensure that the parent constructor is entered.
You might, for instance extend a class where you don't want the parent constructor to be entered. The behavior just allows for more flexibility in design. In most cases, you probably want to call the parent constructor.
It all depends on why you extended the parent. Do you need methods that require the parent to initialize? If yes, call its constructor. Can you just inject certain specific dependencies needed after extending it, or need simple things like constants? Then you probably don't need to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With