Home > Article > Backend Development > What is the usage of php trait?
php trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction. Its usage syntax is such as "trait ezcReflectionReturnInfo {function getReturnType() {/*1* /}...}".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
1. What is the trait in php?
It looks like both a class and an interface, but it is actually neither. Trait can be regarded as a partial implementation of the class, which can be mixed into one or more existing PHP classes. It has two functions: indicating what the class can do. ;Provide modular implementation. Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.
2. PHP version requirements:
PHP5.4 began to introduce traits, whose purpose is to reduce code duplication and increase code reusability.
3. Trait usage scenarios:
Imagine a situation like this. When a method needs to be used in many classes, how to deal with it?
Usually the general approach is to write a base class, implement this method in the base class, and then all classes inherit this base class.
This is a way to deal with it, but it is not the best way to deal with it. Inheritance is usually used when several classes have great similarities. For example, people are a base class, and students, workers, etc. inherit the base class "people" to extend.
From this, the role of trait comes out. Trait can be used in multiple classes.
4. How to use trait:
Quote the example in the PHP manual:
Example 1
<?php trait ezcReflectionReturnInfo { function getReturnType() { /*1*/ } function getReturnDescription() { /*2*/ } } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo; /* ... */ } class ezcReflectionFunction extends ReflectionFunction { use ezcReflectionReturnInfo; /* ... */ } ?>
1. Declare a trait first;
2. Use use in the class to introduce the trait.
Is it very simple (manual escape)? What needs to be noted is the priority of traits.
5. Trait priority
(Knock on the blackboard) Members inherited from the base class will be overridden by members inserted by the trait. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.
Priority: own method > trait method > inherited method (that’s what it looks like.)
Look at the example
<?php trait HelloWorld { public function sayHello() { echo 'Hello World!'; } } class TheWorldIsNotEnough { use HelloWorld; public function sayHello() { echo 'Hello Universe!'; } } $o = new TheWorldIsNotEnough(); $o->sayHello();//输出是 Hello Universe! ?>
Another thing to note is : The use of multiple traits.
<?php trait Hello { public function sayHello() { echo 'Hello '; } } trait World { public function sayWorld() { echo 'World'; } } class MyHelloWorld { use Hello, World; public function sayExclamationMark() { echo '!'; } } $o = new MyHelloWorld(); $o->sayHello(); $o->sayWorld(); $o->sayExclamationMark(); ?>
Summary: Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the usage of php trait?. For more information, please follow other related articles on the PHP Chinese website!