Traits of PHP

WBOY
Release: 2016-07-30 13:31:52
Original
1144 people have browsed it

1. Traits basics

  2. Priority: Methods in the current class will override Trait methods, and Trait methods override methods in the base class.

  3. Multiple Traits, use use statement to list multiple Traits.

  4. Conflict resolution: In order to resolve the naming conflict between two Traits in the same class, you need to use the insteadof operator to explicitly specify one of the conflicting methods. Since this only allows one method to be excluded, the as operator can introduce the conflicting method under another name.

5. Modify method visibility: The class used can adjust the visibility of the method through the as syntax

6. Traits group: Other traits can also use traits, just as classes can use traits. By using one or more traits when a trait is defined, it can combine some or all members of other traits.

 7. Abstract members: In order to impose mandatory requirements on the classes used, Trait supports the use of abstract methods. ​​

​ 8. Traits static members: Static variables can be referenced by trait methods, but cannot be defined by traits. But traits can define static methods for the classes they are used in.

 9. Attributes: If a trait defines an attribute, the class will not be able to define an attribute with the same name, otherwise an error will occur. If the class definitions are compatible (same visibility and initial values) the error level is E_STRICT, otherwise it is a fatal error.

The above has introduced the Traits of PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template