Reputation: 105
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 :)
Upvotes: 9
Views: 8300
Reputation: 3237
The php manual explains it in very simple terms
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. 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).
https://www.php.net/manual/en/language.oop5.decon.php
Upvotes: 0
Reputation:
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.
Upvotes: 3
Reputation: 567
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.
Upvotes: 7
Reputation: 26730
The consequences really depend on what happens in the parent constructor. All public and protected member properties and methods are available even when the parent constructor is not called, that is correct. But the values of inherited class members might be modified/initialized in the parent constructor - you will need to decide whether or not you want that to happen.
Its normally a good decision to always add a call to the parent constructor, even if nothing happens there (that might change in the future). The only reason to omit this call is when you need to override the actions that are taken in the constructor. That's a feature that adds more flexibility than you have e.g. in Java, where invoking the parent constructor is mandatory.
Upvotes: 1