views:

84

answers:

9

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.

It's not like it's vitally important or anything but I'd like to understand this. I've done a lot of reading don't get me wrong, that's why I consider myself retarded and not just lazy.

Help me or shoot me!! Thanks

A: 

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

kgb
I'm trying to call sibling functions, not from the parent but from the childs
johnnyArt
+1  A: 

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.

cmendoza
A: 

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.

codez
A: 

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.

Frank Shearar
So, I could create an object that contains other objects previously created and work with that object instead of the other ones. Those extend a core object that I don't work with and can access the function from their parent but with this object container I could call them all. Overkill? Approaches?
johnnyArt
A: 

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.

Denis 'Alpheus' Čahuk
A: 

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.

timdev
Another way would be to pass instance of second child to contructor in first child, but as it is said earlier, there is no point in that.
Eugene
Right, if both child1 and child2 are going to need invoke methodA, then methodA belongs in the parent.
timdev
A: 

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)
ultrajohn
+1  A: 

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();
}
codez
A: 

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?

Jeriko