Why does php introduce traits instead of multiple inheritance? Why does php introduce traits instead of multiple inheritance?
Reply content:
Why does php introduce traits instead of multiple inheritance?
There are many discussions on the Internet,
WikiMulti-inheritanceIntroduction: https://zh.wikipedia.org/wiki/%E5%A4%9A%E9%87%8D%E7%BB%A7%E6%89%BF
IfClassAAndClassBCommonEat ()Which one do you inherit?
Multi-inheritance is useful as a developer in many cases, but it greatly increases the complexity of both compiler developers and programmers. A problem occurs when two parent classes have data members or methods with the same name. It is hard to solve the problem that is being referenced by the subclass. In addition, when two parent classes inherit from the same base classDiamond problems.
Multi-inheritance has a great impact on the design pattern .. Right
Multi-inheritance violates many design patterns and is not used by most languages.
AlthoughTraitThere are many similarities between multi-inheritance and multi-inheritance. However, they can avoid problems that are easily caused by multi-inheritance, and make it easy to use the design ideas of other languages.Php.