tags:

views:

58

answers:

3

I have three class , called A,B,C and another class called X.

In this X class I want to access a few function from A, B and C.

I know if I want to access the function in one class, I can use:

class X extends A { ... }

Now I can access all A public functions, but now I want to access the B and C class functions as well.

How do I access methods from B and C from within X?

+4  A: 

the only way without interface is

class A extends B {}
class B extends C {}
class x extends A {}

There is no multiple inheritance in PHP

infinity
A: 

PHP does not support multiple inheritance. You can use Composition anyway.

burntblark
+2  A: 

If you're only looking to access the public functions, you might consider rethinking your architecture to use composition instead of inheritance. Tie together your three classes into one class which uses them:

class X {
  private $a, $b, $c;

  public function __construct() {
    $this->a = new A();
    $this->b = new B();
    $this->c = new C();
  }

  function do_stuff() {
    $this->a->do_a_stuff();
    $this->b->do_b_stuff();
    $this->c->do_c_stuff();
  }

}
meagar
then how i call ,
Bharanikumar
@Bhara See updated answer
meagar
can also provide the magic `__call` method to autoproxy method calls to the other instances. that would be kinda slow though because you'll have to check which instance is the right one. On the other hand, this would allow you to define your own rules for the diamond problem. But of course, you could just as well write the appropriate proxy methods on X.
Gordon
yes am looking, thanks, am not tested..test this soon
Bharanikumar