4

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?

Community
  • 1
  • 1
Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • I dont think that implementing multiple inheritance is hard. I think this might be one of the reasons why people write bad code – kirugan Oct 11 '12 at 14:49
  • 1
    @mario: I dont think this is a duplicate of that. The question you pointed out is asking *IF* multiple inheritance is supported in PHP. I know it is not supported. I am asking *WHY* many language designers do not take the trouble to implement it. It is related, but not a duplicate. – Undefined Variable Oct 11 '12 at 14:54
  • Subjective discussion ensues, as we had to guess intentions of the PHP developers here. Anyway, they went with the academic workaround "traits" instead of implementing MI. I'd say that's overcompensation for prior and current language misdesigns. Might also be that the Zend runtime makes it harder to implement MI than better designed scripting cores (CPython). – mario Oct 11 '12 at 14:55
  • @open_sourse, oh. If that's the case, then it should be flagged as "not constructive". Done. – Adi Oct 11 '12 at 14:56
  • Check this: http://stackoverflow.com/questions/90982/multiple-inheritance-in-php?rq=1 – Nathan Oct 11 '12 at 14:56
  • I have written something about it, I hope it helps clarify the problem with multiple inheritance. http://nazar-merza.com/index.php/getting-started/74-the-problem-with-multiple-inheritance – Nazar Merza Oct 11 '12 at 18:52
  • Multiple inheritance is not hard to add, the reason why so many languages don't include it is because it's a bit esoteric and messy. Usually allowing allowing multiple inheritance in code just causes a plethora of bad programming to come from that language usually resulting in a bad reputation for the language. If multiple inheritance is needed for some a reason in that language, a coder who is smart enough to use it properly would likely be smart enough to write their own method that would do it. – DrinkJavaCodeJava Oct 11 '12 at 21:06
  • The problem is not "which of the two parent methods this method overrides"; if you're actually implementing the method in the descendant class, you don't have an inheritance problem at all, because you will not call your inherited methods at all. The problem is if you _don't_ implement `ambiguous_fn` in your `C` class, and then call `$class_C_instance->ambiguous_fn()` — which implementation will be called, `A`'s or `B`'s? – lanzz Oct 11 '12 at 21:29
  • @lanzz: That was an excellent point. I always thought more about the implementing difficulties instead of the definition ambiguities – Undefined Variable Oct 15 '12 at 16:08

1 Answers1

4

Multiple inheritance is not hard to add, the reason why so many languages don't include it is because it's a bit esoteric and messy. Multiple inheritance usually mixes up code, causes poor encapsulation, and makes code behave unpredictably. Usually a allowing multiple inheritance in code just causes a plethora of bad programming to come from that language usually resulting in giving the language a bad reputation. If multiple inheritance is needed for some a reason in that language, a coder who is smart enough to use it properly would most likely be smart enough to write their own method that would do it.