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

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -