Is it always necessary to call the father-class builder in the daughter-class?

Asked

Viewed 160 times

0

created two classes a parent class that has a builder where I have to pass some arguments, and a daughter class that inherits this parent class that I created, but in the daughter class I did not make the builder and I did not use the:

parent::__construct();

But by instilling the child class of error, if I don’t pass the arguments in its header, and when I pass the arguments it works normal and I can use all methods of the parent class.

So I came to the conclusion that, there’s no need to call the father-class builder within the daughter-class, unless the daughter-class has a builder, then I would have to pass on their respective arguments and the parent-class arguments through the Rent::__Construct();.

My conclusion is correct ?

1 answer

1


That’s right, if you are extending the parent’s behavior you must pass the parameters required by it, if you do not need new behaviors in the constructor it is not necessary to call the parent constructor. Calling the parent constructor indicates that vc will override the construction of the object, either to add, remove or change the behavior of the way the object is built.

  • I understood, thank you, this doubt came to me here because in our system that is already old... we usually assemble the class daughters calling the constructor of the parent class this constructor is from the controller of Codeigniter. I’m doing a new project and I didn’t call the builder of the parent class because I didn’t need to modify it and it worked, then I got this doubt, really worth.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.