Reputation: 4446
I'm officially mentally retarded. [Let me explain]
I've never really given classes and their relationship any thought until today. I'm trying to figure out something that seems to be pretty obvious but since I'm stupid I can't see it.
Let's say I have a Core Class that will be extended from different files. How can children classes call functions from other siblings (that is if those are considered childs at all). Sample Code: (don't kill me)
class cOne {
public function functionOne(){
echo "something";
}
}
Then on another file, I say:
class cOneChildOne extends cOne {
public function functionOne(){
echo "something from child one";
}
}
And yet on another file I say:
class cOneChildTwo extends cOne {
public function functionOne(){
echo "something from child two";
}
}
How would I have to create a new object, and when, so that I'm able to access functions from both childs and the parent class in a fashion similar to $newObject->Children->function();
I'm seriously considering the option that I've completely lost my brain today and can't think straight.
I've obviously doing something wrong since: $newObject = new cOne;
creates the object but then the code from one of the subclasses is unable to access anything that's not directly in the core class or in itself.
Thanks
Upvotes: 3
Views: 477
Reputation: 6637
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?
Upvotes: 0
Reputation: 1391
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();
}
Upvotes: 2
Reputation: 2599
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)
Upvotes: 0
Reputation: 62924
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.
Upvotes: 1
Reputation: 4212
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.
Upvotes: 0
Reputation: 17142
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.
Upvotes: 0
Reputation: 1391
Help me or shoot me!!
Bang!!! =o)=o)=o)
When you create instance of class it only knows its methods and method
s from its parents.
There is no way you can tell that there are other class who are extending same parent.
Upvotes: 0
Reputation: 296
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.
Upvotes: 1
Reputation: 11076
you can use parent::functionOne();
to call functions of parent class from child class.
you can't call child classes' functions from parent class.
Upvotes: 0