PHP Multi-inheritance is very confusing, and diamond of death two small middle large inheritance tree, so use trait substitution (clear, realize the reuse code, flattening the way more clear, resolved two semantics (need to execute what method or error).
Last night and G discussed whether a question was using multiple inheritance, and I was very strongly opposed, because multiple inheritance in my influence on the Chinese very poor, think that this is a should be spurned. The main reason is that I think it's very easy to cause all sorts of confusion, programming and design. But for its specific situation is not very understanding, because I have not used multiple inheritance, this is because of the reason for the exam, understand this thing, after the exam immediately lost. In order to understand this thing specifically, I went online Baidu, found the public comment on it. Advantages: For its advantages, is obvious, simple, clear, more conducive to reuse. Although single inheritance can also be useful for reuse, there is a good chance that there will be a lot of duplicate code. If multiple inheritance is used, duplicate code is reduced a lot. For example, Baidu Encyclopedia mentioned an example, a cat, can be inherited at the same time mammals and cartoons, so that you do not have to re-write cartoon class properties and methods. Cons: The disadvantage is also obvious, first of all two semantics, two base classes have the same name method, you have to call in the subclass to indicate that this method originates from that base class. It seems like a bit of a hassle, but the compiler will remind you when you're confused. Second: If Class A derives B and C, and B and C co-derive D, the trouble arises. This middle big two small succession tree has an image name: Diamond Type Inheritance tree (Dod:diamond of Death). This is a very bad word from "death", and the fact is that a is the parent of D, but there are two paths. The way data is organized can have unpredictable consequences. Remove ambiguity don't say, think about it, how many seemingly repeating methods in D, how many data members with the same name! "At all costs, avoid the advent of the DoD. Unless you think the DoD is the most appropriate place to be, and make sure you're using Virtual base classes (virtual inheritance), make sure you're fully aware of the details of each class and make sure you know the side effects of virtual base classes (virtual inheritance). "Here again to introduce a word, vtable (c + +, PHP is not very clear, no Baidu to, but there should be.") ) Vtable the virtual table. Every class with a virtual function has such a thing. It actually records the function pointers of all virtual functions in this class, that is, the starting position of a function pointer array. For example, virtual void Thesecondfun () is recorded in the second element of the array, and when an object instance of that class calls Thesecondfun, the second function pointer is taken out according to the corresponding relationship, and then the function is called late binding, That is, at run time, you know what functions are called, not early bindings that are determined during the compilation phase. Multiple inheritance also makes vtable of subclasses more unusual. Single-inherited vtable are only vtabl in the parent classThe footer of E is added with a new virtual function, and the vtable of the subclass object contains the ordered parent class vtable. For multiple inheritance, two parent classes may have completely different vtable, so it is absolutely impossible for a subclass of vtable to contain the complete vtable of an ordered two parent class. The subclass's vtable may contain two unconnected parent class vtable, so each parent class is forced to append a vtable, that is, each parent class object has a pointer added to it. Multiple inheritance also brings some other problems: using a parent pointer to a subclass object becomes a complex thing. You have to use the dynamic_cast provided in C + + to perform the cast. As for dynamic_cast, a troublesome fellow, it is converted during runtime rather than during compilation (because it cannot determine exactly what type to turn to during compilation), so in addition to some slight performance penalty, it requires the compiler to allow Rtti (runtime Type Information, run-time type information), which requires the compiler to save information about all classes at run time. Summary: In real life, some things tend to have two or more than two things, in order to solve this problem, introduced the concept of multiple inheritance. The advantage of multiple inheritance is that an object can call an interface in more than one base class. The disadvantage of multiple inheritance is that it is easy to inherit up to two semantics and increase the complexity of the program. Recommendation: Use multiple inheritance judiciously wisely and cautiously using multiple inheritance. The last of the last: it is reasonable to exist.