I know in Java, super()
in a constructor has to be called as the first line of an overridden constructor.
Does this also apply to the parent::__construct()
call in PHP?
I found myself writing an Exception class like this:
class MyException extends Exception {
public function __construct($some_data) {
$message = '';
$message .= format_data($some_data);
$message .= ' was passed but was not expected';
parent::__construct($message);
}
}
and I wondered if this would be considered an error/bad practice in PHP.
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.
Cases-1: Constructor call order in single inheritance java In simple words, we can say that the parent constructor gets called first, then of the child class.
To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
We can do this by using the special function call parent::__construct(). The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor".
If you want the code in the parent's constructor to be executed, you need to call parent::__construct(…)
at some point. It does not technically matter when you do so. Sometimes it makes more sense to do a little work in the overridden constructor before calling the parent's constructor, sometimes you rely on work the parent's constructor does before you can do work in the overridden constructor.
As a rule of thumb I'd say you should call the parent's constructor as soon as possible. If you need to do something before you can call the parent's constructor, do so. If not, call it immediately. This is to avoid the parent's constructor undoing any of the work you're doing in the overridden constructor, like setting certain properties for example.
class A {
function __construct() {
$this->foo = 'bar';
}
}
class B extends A {
function __construct() {
// parent::__construct();
$this->foo = 'baz';
// parent::__construct();
}
}
In the above sample, the difference between calling the parent first or last makes a big difference in the resulting object. Which is more appropriate depends on what you're trying to do.
The main question is not whether the call to the parent constructor it should be executed on the first line, or executed at all. Instead it's about what can be considered better practice and why.
Doubling the given answers: yes, you can omit parent::__construct
altogether; yes, you can call it from wherever you like inside the __construct
function of the child class. And any approach is good, as long as it makes sense and is consistent.
If you have a parent class with three children, for example, and every child uses the parent::__construct
call in it's own way, then it can be a flag that the inheritence is not too clear and should be refactored.
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