Trait features of PHP-Zhao Yafei

Source: Internet
Author: User
[Switch] Trait feature of PHP-Zhao Yafei Trait is added in PHP5.4, which is neither an interface nor a class. The main purpose is to solve the restrictions of the single inheritance language. It is a solution for PHP multi-inheritance. For example, it is very troublesome to inherit two Abstract classes at the same time. Trait is designed to solve this problem. It can be added to one or more existing classes. It declares what the class can do (indicating its interface features), and also contains specific implementations (indicating its class features)

Easy to use

First, of course, we declare a Trait, and PHP5.4 addsTraitKeywords

1 trait first_trait {2      function first_method() { /* Code Here */ }3      function second_method() { /* Code Here */ }4 }

If you want to use this Trait in the Class, use the use keyword.

1 class first_class {2 // pay attention to this line and declare to use first_trait 3 use first_trait; 4} 5 6 $ obj = new first_class (); 7 8 // Executing the method from trait 9 $ obj-> first_method (); // valid10 $ obj-> second_method (); // valid

Use multiple Trait

Multiple Trait instances can be used in the same Class.

 1 trait first_trait 2 { 3     function first_method() { echo "method1"; } 4 } 5  6 trait second_trait { 7     function second_method() { echo "method2"; } 8 } 9 10 class first_class {11     // now using more than one trait12     use first_trait, second_trait;13 }14 15 $obj= new first_class();16 17 // Valid18 $obj->first_method(); // Print : method119 20 // Valid21 $obj->second_method(); // Print : method2

Nesting between Trait

Meanwhile, Trait can also be nested with each other, for example

 1 trait first_trait { 2     function first_method() { echo "method1"; } 3 } 4  5 trait second_trait { 6     use first_trait; 7     function second_method() { echo "method2"; } 8 } 9 10 class first_class {11     // now using 12     use second_trait;13 }14 15 $obj= new first_class();16 17 // Valid18 $obj->first_method(); // Print : method119 20 // Valid21 $obj->second_method(); // Print : method2

Abstract Method of Trait)

We can declare the abstract method to be implemented in Trait so that the Class that uses it must implement it.

1 trait first_trait {2 function first_method () {echo "method1";} 3 4 // Here you can add modifiers, indicating that the call class must implement its 5 abstract public function second_method (); 6} 7 8 class first_method {9 use first_trait; 10 11 function second_method () {12/* Code Here */13} 14}

Trait conflict

When multiple Trait nodes are used at the same time, they will inevitably conflict with each other. we need to solve this problem. PHP5.4 introduces related keyword syntax in terms of syntax:InsteadofAndAsFor usage, see

1 trait first_trait {2 function first_function () {3 echo "From First Trait"; 4} 5} 6 7 trait second_trait {8 // The name here is the same as first_trait, 9 function first_function () {10 echo "From Second Trait"; 11} 12} 13 14 class first_class {15 use first_trait, second_trait {16 // here it is declared to use first_function of first_trait to replace 18 first_trait: first_function insteadof second_trait stated in 17 // second_trait; 19} 20} 21 22 $ obj = new first_class (); 23 24 // Output: From First Trait25 $ obj-> first_function ();

The above are some basic usage of Trait. For more details, refer to the official manual. Here are some notes:

  • Trait overwrites the parent class method inherited by the call class.
  • Trait cannot be instantiated using new like Class
  • A single Trait can be composed of multiple Trait
  • In a single Class, you can use multiple Trait
  • Trait supports modifiers, such as final, static, and abstract
  • We can use the insteadof and as operators to solve Trait conflicts.

Article Source: http://www.kuqin.com/web/20111119/315048.html

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.