Answer the question
In order to leave comments, you need to log in
How does PHP solve the problem of multiple inheritance?
How does PHP solve the problem of multiple inheritance? I know that multiple inheritance is solved using trait , but the question is how the problem is solved if, for example, we have 3 identical methods or properties, i.e. how is this problem solved? Which class is preferred?
Answer the question
In order to leave comments, you need to log in
Yes, inheritance is wrong in general. It was necessary to do like people do - mom and dad. That is, so that the class can only inherit from two classes and nothing else. Thus, we have multiple inheritance even to infinity. There was not enough mom and dad, add grandparents, you can have 2 pieces, etc. The only thing you need is to add the gender of the class and modifiers for fields and methods to the language that will set priority if both parents have it, if both, then we take my father's option (patriarchy). So it would be possible to visually combine different mothers and fathers in order to give birth to children with the necessary habits.
php.net/manual/ru/language.oop5.traits.php
See examples,
(the link says nothing about several traits)
I wrote down a test script, with collisions in the names of methods of different traits, a fatal error is thrown:
UPD:
PHP Fatal error: Trait method sayHello has not been applied, because there are collisions with other trait methods on MyHelloWorld in .... on line ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question