: This article mainly introduces the new feature 3 Traits in chapter 2 of [ModernPHP]. if you are interested in the PHP Tutorial, refer to it. Traits
Many of my PHP developers do not know traits very well. this is a new concept introduced in PHP 5.4.0. Traits looks like an interface but uses a class. What exactly is it? Neither of them.
A trait has partial implementations (such as constants, attributes, and methods) and can be implanted into one or more actual PHP classes. Trait has two responsibilities: to indicate what a class can do (similar to an interface); to provide a modular implementation (similar to a class ).
You may have some knowledge about traits in other languages. For example, Ruby's modules and mixins functions are similar to PHP's traits.
Why should we use traits?
The PHP language uses a classic inheritance model. This means that you start with a general root class that provides basic implementation. Create more specific classes from the root class, which directly inherits various implementations of the parent class. This is called an inheritance layer. many programming languages use this common mode.
For ease of understanding, suppose you go back to high school to learn biology. Do you still remember the species and species of the creature you learned? There are a total of six realms, namely, out-of-the-box, out-of-the-box, out-of-class, out-of-class, and out-of-class, and under-class. Each downward derivation at the species level represents a specific feature.
The classic inheritance model works well in most cases. However, if two unrelated classes need to implement similar behavior, how can this problem be solved? For example, one PHP class is RetailStore, and the other PHP class is Car. The two classes can be completely different from each other, A common parent class cannot be shared in the inheritance relationship. However, both types use the longitude and latitude in the geographical location to display the map coordinates.
We create traits to solve this problem. They can inject some implementations into irrelevant classes. Traits is also conducive to code reuse.
In this case, my first solution (and worst) was to create a common parent class Geocodable to inherit from the RetailStore and Car classes. This solution is too bad, because it forces two unrelated classes to share a common ancestor, which is especially awkward at their respective inheritance levels.
My second solution (slightly better) is to create a Geocodable interface to define the methods required to implement geographic location. Both the RetialStore and Car classes can implement this Geocodable interface. It is indeed a good solution to keep the natural inheritance relationships of each class. However, we still need to implement interface definitions repeatedly in each class, which is not a DRY solution.
DRY is the abbreviation of Do not repeat yourself. As a good programming habit, we should never repeat the same code in multiple places. You cannot modify the same code in other places because you have changed the code.
My third solution (the best solution) is to construct a Geocodable trait, which defines and implements relevant methods. I can add the trait of Geocodable to the RetailStore class and Car class without disrupting the class hierarchy.
How to construct a trait
The following shows how to define a PHP trait:
As a good habit, we should make a file a trait, just like the definition of classes and interfaces.Let's return to our Geocodable example to better demonstrate the use of trait. We all know that the RetailStore and Car categories need to support location locating, and we can all agree that inheritance and interfaces are not the best solutions. Instead, we construct a Geocodable trait to return the longitude and latitude coordinates that can be marked on the map. In example 2-12, the complete Geocodable trait is used.
Example 2-12 Definition of Geocodable trait
geocoder = $geocoder; } public function setAddress($address) { $this->address = $address; } public function getLatitude() { if (isset($this->geocoderResult) === false) { $this->geocodeAddress(); } return $this->geocoderResult->getLatitude(); } public function getLongitude() { if (isset($this->geocoderResult) === false) { $this->geocodeAddress(); } return $this->geocoderResult->getLongitude(); } protected function geocodeAddress() { $this->geocoderResult = $this->geocoder->geocode($this->address); return true; }}
Geocodable trait only defines the attributes and methods required to implement the geographic location function without any additional features.Geocodable trait defines the attributes of three classes:
To be continued ......
The above introduces [Modern PHP] Chapter 2 new feature 3 Traits, including some content, and hopes to help those who are interested in PHP tutorials.