Simple use of traits in PHP, phptraits instance _ PHP Tutorial

Source: Internet
Author: User
PHP traits is a simple use instance and phptraits instance. The traits in PHP is a simple application instance. the traits in phptraits instance PHP5.4 is a newly introduced feature. Chinese really do not know how to translate it accurately. The actual purpose is to make some PHP traits simple use instances and phptraits instances

Traits in PHP 5.4 is a newly introduced feature. Chinese really do not know how to translate well. The actual purpose is to use multi-inheritance for some occasions, but PHP does not inherit much, so it invented such a thing.
Traits can be understood as a set of methods that can be called by different classes, but Traits is not a class! It cannot be instantiated. Let's take an example to see the syntax:

<? Phptrait myTrait {function traitMethod1 () {} function traitMethod2 () {}} // then call this traits. Syntax: class myClass {use myTrait ;} // you can use myTraits to call the methods in Traits, for example, $ obj = new myClass (); $ obj-> traitMethod1 (); $ obj-> traitMethod2 ();>

Next, let's explore why traits should be used. for example, there are two classes: business (business) and Inpidual (personal). they both have the attribute of the address, the traditional approach is to abstract a parent class with features of both classes, such as the client. in the client class, set the access attribute address, and inherit the business and inpidual respectively, the following code:

// Class Client class Client {private $ address; public getAddress () {return $ this-> address;} public setAddress ($ address) {$ this-> address = $ address ;}} class Business extends Client {// Here you can use the address attribute} // Class Inpidual class Inpidual extends Client {// Here you can use the address attribute}

But what if another one is called the order class and needs to access the same address attribute? The order class cannot inherit the client class because it does not conform to the OOP principle. In this case, traits can be used to define public attributes.

// Trait Addresstrait Address {private $ address; public getAddress () {eturn $ this-> address;} public setAddress ($ address) {$ this-> address = $ address ;}// Class Businessclass Business {use Address; // you can use the address attribute here} // Class Inpidualclass Inpidual {use Address; // here the address attribute can be used} // Class Orderclass Order {use Address; // here the address attribute can be used}

This is much more convenient!

Traits in PHP 5.4 is a newly introduced feature. Chinese really do not know how to translate it accurately. The actual purpose is to have some places...

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.