17

Is Multiple Inheritance allowed at class level in PHP?

OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • Possible duplicate, http://stackoverflow.com/questions/90982/multiple-inheritance-in-php – Anthony Forloney Apr 22 '10 at 12:57
  • I believe the accepted answer should change here, because there is a workaround for `multiple inheritance` in PHP. Which is described in [this answer](https://stackoverflow.com/a/46905316/2394254) but not in the accepted answer. – mega6382 Nov 17 '17 at 12:01

4 Answers4

42

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:

Gordon
  • 312,688
  • 75
  • 539
  • 559
4

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

mega6382
  • 9,211
  • 17
  • 48
  • 69
3

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);
    }
}
mega6382
  • 9,211
  • 17
  • 48
  • 69
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
1

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

Homesh Paul
  • 141
  • 9