0

Would this be considered a good way to make use of a parent class's method through a child class?

Parent:

$protected $html;

Child:

parent::__construct($url);       //set $html

//do something with $html

parent::__construct($new_url);   //overwrite existing $html

//do something with $html

1 Answer 1

5

Things that go into the constructor usually aren't supposed to change during the lifetime of the object. The constructor has a very special purpose. It is called once and only once per object, when the object is created. You shouldn't just keep calling it like any other method, even if PHP allows you to do it.

Also, I assume your "child" code is in the child's constructor? (If not, it's bad practice to call the parent's constructor anywhere in the child except the child's own constructor.)

It's probably neater to destroy the old object and create another one with the new $url. That's how constructors are supposed to be used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.