el_pup_le
el_pup_le

Reputation: 12179

PHP class design

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

Upvotes: 0

Views: 403

Answers (1)

kijin
kijin

Reputation: 8900

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.

Upvotes: 5

Related Questions