In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to solve the problem of PHP single inheritance. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to Trait
Trait is a new concept introduced by PHP 5.4.It looks like both classes and interfaces.
Not really. Trait can be seen as a partial implementation of a class and can be mixed into one or more existing PHP classes.
Trait is a kind of code reuse technology, which provides a set of flexible code reuse mechanism for the single inheritance limitation of PHP.
Trait action
Indicates what a class can do
Provide modular implementation.
Single inheritance structure
I mentioned at the beginning that PHP is a single inheritance language, and our usual habit is:
First write a general base class, implement the basic functions, encapsulate the general logic, and then extend the base class
Then create a more specific subclass that inherits the implementation directly from the parent class.
This is called the single inheritance hierarchy, and many programming languages use this pattern.
This typical inheritance model works well most of the time, but what if you want two unrelated PHP classes to have similar behavior?
Sample source code
I queried the source code of Laravel. Take the self-contained LoginController as an example, where login authentication is realized through Trait:
Create Trait
Creating a Trait is simple, similar to creating a class, except that the keyword is trait instead of class. Take the AuthenticatesUsers above as an example:
What we define through the trait declaration is a Trait, and then we can define the properties and methods to be used in this Trait like a class.
In addition, Trait supports nesting and composition, that is, combining one or more Trait (multiple uses, separations) into a Trait, such as AuthenticatesUsers:
Note: abstract and static methods are also supported in Trait, where abstract methods must be implemented in classes that use it.
Priority of the calling method:
Calling class > Trait > parent class (if any), the method can be overridden, but the property is not.
Note: if an attribute is defined in Trait, an error will be reported if it is also defined in the calling class.
Use Trait
The use of Trait is also simple, as shown in the screenshot example above: use the use keyword.
Lead-in position
Note: both namespaces and Trait are introduced using the use keyword, except that the namespace is imported outside the class definition, while Trait is imported inside the class definition.
Compilation problem
If Trait and the class referencing Trait have the same properties and methods, we need to use the instansof keyword to indicate whether the method is class or Trait when we call it externally.
Because the PHP interpreter will copy the Trait into the definition body of the class at compile time, but will not deal with the incompatibility problem introduced by this operation, we need to solve the compatibility problem.
Thank you for reading! This is the end of this article on "how to solve the problem of PHP single inheritance". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.