views:

157

answers:

6

Is Multiple Inheritance allowed at class level in PHP?

+1  A: 

PHP does not support multiple inheritance.

deceze
A: 

PHP doesn't not support multiple inheritance for classes.

cletus
+8  A: 

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
Great description Thanks Gordon!!!
OM The Eternity
wooooooo, graphics! +1.
Manos Dilaverakis
+1  A: 

No, PHP classes can only inherit one class, not multiple.

Kai Sellgren
+1  A: 

No, but:

http://stackoverflow.com/questions/90982/multiple-inheritance-in-php

zaf
Thanks Mate!!!!
OM The Eternity
A: 

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 = array($this->otherParent, $method);

        return call_user_func_array($method, $args);
    }
}
Ionuț G. Stan
PLs elaborate I dindt understood the point u r talking about...
OM The Eternity
Which point? The is_a point? Or the delegation?
Ionuț G. Stan
Both Pls eloborate Both point, I am a new coder not aware of these concepts
OM The Eternity