Understanding Classes in PHP

南楼画角 提交于 2019-12-05 19:57:19

You can collect child instances in masters class static array

class C1{  
  static public $childs=array();
  function __construct(){
    self::$childs[]=$this;    
  }
}
class C1C1 extends C1{
  function hi(){
    echo 'hi from C1C1 <br />';
  }  
}
class C1C2 extends C1{
  function hi(){
    echo 'hi from C1C2 <br />';
  }    
}

$c1 = new C1C2();
$c2 = new C1C2();
$c3 = new C1C1();
$c4 = new C1C1();
$c5 = new C1C1();
$c6 = new C1C1();
foreach(C1::$childs as $c){
  $c->hi();
}

The parent class cOne has no knowledge of the classes that extend it in php, so while you can call to the parent from a child class using parent::someFunction(), you cannot call the child classes from the parent. You also could not call functions from other classes that extend cOne from a class that extends cOne, also because cOne has no knowledge of classes that extend it.

you can use parent::functionOne(); to call functions of parent class from child class. you can't call child classes' functions from parent class.

Help me or shoot me!!

Bang!!! =o)=o)=o)

When you create instance of class it only knows its methods and methods from its parents. There is no way you can tell that there are other class who are extending same parent.

As kgb says, you can't create one object that will give you "access" to both sibling class' behaviour. If you instantiate a cOneChildOne, you'll get something that outputs "something from child one" (*). You could, if you use parent::functionOne(), copy cOne's behaviour, maybe to return "something\nsomething from child one" or whatever.

(*) Don't do this. Rather, return a string:

public function functionOne(){
   return "something from child one";
}

This lets you compose your functions, output them to files, etc.

You could always just call cOneChildTwo from inside cOneChildOne statically if this suffices your requirement. $this in a method always points to the callee object (that's how parent::__construct() works), so you could use the callee's state inside cOneChildTwo for extended behaviour.

However, this would possibly imply that you'd require wrapper methods for every sibling's method. This is nothing Reflection can't solve though.

You do have a fundamental misunderstanding.

Your two subclasses are different classes with a common ancestor. Each child essentially has knowledge of the parent, but the parent has no knowledge of the children, and the children have no knowledge of each other.

If you want child1 to be able to call methods of child2, then there is something wrong with your design. One solution would be to move the method from child2 to the parent class, so that child1 would also inherit that method.

i think you're looking at object inheritance, classes and their relationships the wrong way.. what you've said is not really how inheritance in object oriented programming works. when you want to call a method/function of some other object, may it be an instance of the same class or not, what you need is a reference to that object that you are going to call. the keyword here is pass a reference. the one wanting to call must have a reference to the object it wants to communicate to.

 child1.communicateWithAnotherChild(someOtherChild)

Why can't you have a static Array $children in the cOne class, and the __construct() methods for the classes that extend it just call cOne::addChild($this)? They could each implement a getChildren function which sends the request on to the superclass - Shouldn't that work?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!