Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must I call parent::__construct() in the first line of the constructor?

Tags:

oop

php

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.

like image 795
AndreKR Avatar asked Sep 28 '16 12:09

AndreKR


People also ask

Do you have to call parent constructor?

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.

Is parent or child constructor called first?

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.

How do you call a parent class constructor?

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.

What does parent __construct () do?

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".


2 Answers

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.

like image 57
deceze Avatar answered Oct 17 '22 09:10

deceze


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.

like image 1
BVengerov Avatar answered Oct 17 '22 09:10

BVengerov