开发者

php Extends three class without interface

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 开发者_StackOverflow中文版B and C class functions as well.

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


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


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

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜