D
D
deepin2019-07-24 15:42:25
PHP
deepin, 2019-07-24 15:42:25

Is it necessary to specify parent::__constructor in the class being extended if it exists in the parent?

I didn’t find this in the official documentation, but I read in some article that if the parent has a constructor, then if we need it in the extensible class , then we specify parent::__constructor.
And if not needed, is it necessary to specify parent::__constructor?
The matter is that it is not necessary to me, but the storm swears: there is no parent constructor.

piu
5d3852227c250380230986.png

Does it have to be specified?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1210mk2, 2019-07-24
@deepin

did you read everything carefully ?

P
profesor08, 2019-07-24
@profesor08

If something is done in the parent's constructor, then it must be called from the child's constructor. If you do not call the parent constructor, then you must clearly understand what and why you are doing this.

class Constroller {
    public function __construct() {
        echo "Constroller-";
    }
}

class Home extends Constroller {
    public function __construct() {
        echo "Home\n";
    }
}

class About extends Constroller {
    public function __construct() {
        parent::__construct();
        echo "About\n";
    }
}

new Home();
new About();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question