Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happen if you don't call parent constructor explicitly in PHP?

Tags:

php

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 :)

like image 695
Felix Perdana Avatar asked Jun 07 '12 05:06

Felix Perdana


People also ask

Does PHP automatically call parent constructor?

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().

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.

Why is it necessary to call the constructor of the parent class before the child class?

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.

Does child constructor call parent constructor?

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.


2 Answers

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.

like image 191
raPHPid Avatar answered Sep 21 '22 09:09

raPHPid


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.

like image 24
Tim Post Avatar answered Sep 23 '22 09:09

Tim Post