Is Multiple Inheritance allowed at class level in PHP?

旧城冷巷雨未停 提交于 2019-12-17 10:34:35

问题


Is Multiple Inheritance allowed at class level in PHP?


回答1:


Multiple inheritance suffers from the Diamond Problem, which has not been (agreed upon how to be) solved in PHP yet. Thus, there is no multiple inheritance in PHP.

    BaseClass
       /\
      /  \
 ClassA  ClassB
      \  /
       \/
     ClassC

If both ClassA and ClassB defined their own method foo(), which one would you call in ClassC?

You are encouraged to either use object composition or interfaces (which do allow multiple inheritance) or - if you are after horizontal reuse - look into the Decorator or Strategy pattern until we have Traits (or Grafts or whatever they will be called then).

Some Reference:

  • [PHP-DEV] Traits,Grafts, horizontal reuse
  • [PHP-DEV] Multiple class inheritance
  • RFC: Traits for PHP
  • Traits-like Functionality in PHP now



回答2:


You can mimic it using method and property delegation, but it will not work with is_a() or instanceof:

class A extends B
{
    public function __construct($otherParent)
    {
        $this->otherParent = $otherParent;
    }

    public function __call($method, $args)
    {
        $method = [$this->otherParent, $method];

        return call_user_func_array($method, $args);
    }
}



回答3:


PHP does not support multiple inheritance as such, but it does provide some ease to reuse sets of methods in multiple independent classes, using traits. A trait is written just like a class, but it cannot be instantiated by itself.

below are a couple of examples from PHP Manual:

Precedence Order Example:

class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();

Output:

Hello World!

Here is another example with conflict resolution:

trait A {
    public function smallTalk() {
        echo 'a';
    }
    public function bigTalk() {
        echo 'A';
    }
}

trait B {
    public function smallTalk() {
        echo 'b';
    }
    public function bigTalk() {
        echo 'B';
    }
}

class Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
    }
}

class Aliased_Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
}

For more information and deeper understanding of Multiple Inhertance in PHP.

Look into Horizontal Reuse for PHP




回答4:


No, PHP don't support multiple inheritance.

To allow this feature in PHP, you can use interfaces or you can use "Traits" instead of classes.

PHP 5.4.0 comes with traits, which will fulfill multiple inheritance limitation. Read more about traits from the given link below :

http://php.net/manual/en/language.oop5.traits.php



来源:https://stackoverflow.com/questions/2690898/is-multiple-inheritance-allowed-at-class-level-in-php

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