I remember there was an interview question asking whether PHP supports multiple inheritance?
Answer: No, only single inheritance is supported.
How to implement multiple inheritance?
Answer: It can be implemented using interface or trait. (Recommended learning: PHP Programming from entry to proficiency)
Why do you think of this problem? Because if a class inherits multiple interfaces, then they will have the same properties and methods. Whose methods or properties are referenced, and who will be overridden?
Summary:
1. Use interface to declare that the class cannot be instantiated, and the attributes must be constants, and the method cannot have a method body
2. The class declared by the trait cannot be instantiated. It is introduced by use and will overwrite the same properties and methods of the parent class. If there are multiple uses, the following ones will overwrite the same properties and methods on the top in order
What is the interface?
Using interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.
The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.
All methods defined in the interface must be public. This is a characteristic of the interface.
What is trait?
It looks like both a class and an interface, but it is actually neither. Trait can be regarded as a partial implementation of a class and can be mixed into a or multiple existing PHP classes,
has two functions: to indicate what the class can do; to provide modular implementation.
Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.
Trait reduces the limitations of single inheritance languages and enables developers to freely reuse methods in independent classes within different hierarchies. The semantics of the combination of Trait and Class define a way to reduce complexity and avoid Typical issues related to traditional multiple inheritance and Mixin classes.
Trait is similar to Class, but is only designed to combine functionality in a fine-grained and consistent way. Cannot be instantiated through the trait itself. It adds a combination of horizontal features to traditional inheritance; that is, there is no need for inheritance between several Classes in an application.
Priority
Members inherited from the base class are overridden by members inserted by the trait. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.
class Base { public function sayHello() { echo 'Hello '; } } trait SayWorld { public function sayHello() { parent::sayHello(); echo 'World!'; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(); $o->sayHello();
Separated by commas, list multiple traits in the use statement, which can all be inserted into a class.
The above is the detailed content of How to use multiple inheritance in PHP. For more information, please follow other related articles on the PHP Chinese website!