php - Calling a child object in the parent object -
i want know if can create child object in parent object use it...
- i mean posible?
- is idea?
- what have care if so?
i using child classes on own , want use them in private function of parent class well.
thanks
here source imagine meaning:
class a{ private $child_class1; private $child_class2; private function somefunction(){ $this->child_class1 = new b(); $this->child_class1->do_something(); $this->child_class2 = new c(); $this->child_class2->do_something(); } } class b extends a{ public function do_something(){ ... } } class c extends a{ public function do_something(){ ... } }
this seems bad idea imho - it's going require high maintenance , creating tight couplings between classes.
i recommend creating abstract function in parent each of children implement it's own logic.
[edit] since trying iterate on child objects recommend create base class handles logic needs implemented children, , override in each of child classes need additional logic, , call parent function inside it.
Comments
Post a Comment