Is Multiple Inheritance allowed at class level in PHP?
+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
2010-04-22 12:57:20
Great description Thanks Gordon!!!
OM The Eternity
2010-04-22 13:04:58
wooooooo, graphics! +1.
Manos Dilaverakis
2010-04-22 13:37:40
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
2010-04-22 13:04:45
Both Pls eloborate Both point, I am a new coder not aware of these concepts
OM The Eternity
2010-04-22 13:11:01