What are traits? Application Scenarios for PHP traits

Source: Internet
Author: User
Tags php language php class traits
What this article brings to you is about what is a trait? PHP Characteristics of the application scenario, there is a certain reference value, the need for friends can refer to, I hope to help you.

Why use traits?

The PHP language uses a typical inheritance model. In this model, we first write a generic root class, implement the basic functions, and then protect the root class, create more specific classes, from the direct parent class to inherit the implementation. This is called an inheritance hierarchy, and many programming languages use this pattern.

Most of the time, this typical inheritance model works well. But what if you want two unrelated PHP classes to behave in a similar way? For example, Retailstore and car two PHP classes have very different roles, and there is no common parent class in the inheritance hierarchy. However, these two classes should be able to use geocoding techniques to convert to latitude and longitude, and then display them on a map.
Traits are created to solve this problem. Traits can be implemented in a modular way into many unrelated classes. And traits can also facilitate code reuse.
To solve this problem, my first reaction was to create a parent class geocodable (this is not good) and let both Retalstore and car inherit this class. This workaround is not good because we force two unrelated classes to inherit the same ancestor, and it is clear that the ancestor does not belong to the respective inheritance hierarchy.
My final response is to create geocodable traits (best of all), to define a well to implement the two-class native method of the geocoding state, and then to mix the characters in Retailstore and car two classes. Doing so does not stir the inheritance hierarchy of ancient nature.

For example

We want the two classes of Retailstore and car to provide geocoding capabilities, and recognize that inheritance and interfaces are not optimal scenarios. The solution we chose is to create geocodable traits, return latitude and longitude, and then draw in the map. The geocedable traits are defined as follows:

?phptrait geocodable {/** @var string */protected $address;/** @var \geocoder\ Geocoder */protected $geocoder,/** @var \geocoderlresult\geocoded */protected $geocoderResult;p ublic function Setgeocoder (\geocoder\geocoderintertace $geocoder) {$this->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 traits only need to define the properties and methods required to implement the Geocoding function, and nothing else is required, this geocodable trait defines three class attributes: one for the address (string), one for the Geocoding object, and one for the result object after the geocoding process. We have also defined four public methods and a protected method. The Setgeocoder () method is used to inject geocoder objects; The Setaddress () method is used to set the address; The Getlatitude () and Getlongitude () methods return latitude and longitude respectively; The Geocodeaddress () method passes the address string to the Geocoder instance and obtains the results obtained by the geocoding process.
How do I use traits?

PHP character usage is very simple, use mytrait, the statement is added to the definition of the PHP class can be. Here is an example. Obviously, the actual use of the mytrait to replace the corresponding PHP character name.

<?phpclass myclass{use    mytrait;    This is the implementation of the class}

Recommendation: Namespaces and traits are guided by the USE keyword, but the location of the guide is different. namespaces, classes, interfaces, functions, and constants are in the definition of a class, whereas traits in the definition of a class guide the human body. This difference is small, but it is very important. and using use is the premise that you already include the PHP file.

We just need to do so much. Now, each Retailstore instance can use the properties and methods provided by the geocodable trait, namely:

$store = new Retailstore (); $store->setddress (' 420 9th Avenue, new York, NY 10001 USA ');

The PHP interpreter will copy and paste traits into the definition body of the class at compile time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.