Possible Duplicate:
Is Multiple Inheritance allowed at class level in PHP?
So I know that in PHP, multiple inheritance is not "directly" supported (I guess this is the case in many other languages like java, for instance). So we do something like:
class A extends B implements C, D
and now from PHP version 5.4
, I think, we have traits
which bunches functionality together..
and can be used
by multiple classes.
Now my question is this, why can't there be multiple inheritance support in such languages?
I mean something like:
class A extends B, C
I have read some articles on the internet where people were trying to explain why this was hard to implement in the language, but either they were too technical for me to understand or did not sound like a good enough argument. Can someone describe in simple terms why it is hard to support multiple inheritance in these languages? It would be great if you can put in the context of PHP because that is the language I know the best.
I always keep thinking it could be because of ambiguity (Diamond problem et al), but that could be easily be handled by some kind of namespace tokenizer mechanism while declaring the function, I'd think.. I know that this would mean that the language might need to have the same function redefined twice, (like how overloading works in Java)
<?php
class A{
function __construct()
{
}
function ambiguous_fn()
{
echo "This function is ambiguous";
}
function fnA()
{
echo "This is function A";
}
}
class B{
function __construct()
{
}
function ambiguous_fn()
{
echo "This function is ambiguous";
}
function fnB()
{
echo "This is function B";
}
}
class C extends A,B{
function __construct()
{
}
//This function will obviously confuse the PHP engine, because it does not know
//if it should override from A or B. But cant we have a namespace/tokenizer kind of solution
//to resolve the ambiguity? Or is that not possible?
function ambiguous_fn()
{
}
}
?>
I mean from a real-life object mimicking perspective, at times I think that pure multiple inheritance will be such a great help. I know that one of the argument is that "The use case for multiple inheritance is very small, and can usually be resolved using multiple interfaces, mixins, etc". But really, there has to be a more valid reason why the language designers have not bothered to implement pure multiple inheritance one would think.
Am I totally missing the point here? Can someone shed more light on this?