Home > Article > Backend Development > An in-depth analysis of PHP traits
Since PHP5.4.0, PHP has implemented a code reuse method called trait
.
As we all know, PHP has single inheritance, and trait is a code reuse mechanism prepared for single inheritance languages similar to PHP. Traits are designed to reduce the limitations of single-inheritance languages and allow developers to freely reuse methods in independent classes within different hierarchies. The semantics of trait and class composition define a way to reduce complexity and avoid traditional multiple inheritance problems.
trait
is similar to class
, but only combines functionality in a fine-grained and consistent way. Cannot be instantiated through the trait itself. It adds a combination of horizontal features to traditional inheritance; that is, there is no need for inheritance between several classes in an application.
Recommended PHP video tutorial: https://www.php.cn/course/list/29/type/2.html
1. Simple Example
First we give an example to introduce trait, which is similar to class definition, but uses the keyword trait definition. Use use combinations in classes.
<?php trait T{ public function t1(){ return 't1'; } public function t2(){ return 't2'; } } class Demo{ use T; public function test(){ return 'test'; }多个 } $demo = new Demo; echo $demo->t1(),'</br>'; echo $demo->test();
Output result:
t1
test
2. Multiple trait conflict resolution
If multiple traits are used, but the method names are the same, there will be a conflict. You must manually specify which trait method to use, and use the insteadof
keyword to achieve this.
<?php trait T{ public function t1(){ return 't1'; } public function t2(){ return 't2'; } } trait T2{ public function t1(){ return 't3'; } } class Demo{ use T,T2{ //使用T的m1方法 T::t1 insteadof T2; } public function test(){ return 'test'; } } $demo = new Demo; echo $demo->t1(),'</br>'; echo $demo->test();
Output result:
t1
test
In the event of a conflict, you can also use use to define method aliases to resolve the conflict. The example is as follows:
<?php trait T{ public function t1(){ return 't1'; } public function t2(){ return 't2'; } } trait T2{ public function t1(){ return 't3'; } } class Demo{ use T,T2{ //使用T的m1方法 T::t1 insteadof T2; T2::t1 as new_t1; } public function test(){ return 'test'; } } $demo = new Demo; echo $demo->t1(),'</br>'; echo $demo->new_t1(),'</br>'; echo $demo->test();
Output result:
t1
#t3
test
3. Change access permissions
You can also use the use
keyword to change the access permissions of the method.
<?php trait T{ public function t1(){ return 't1'; } public function t2(){ return 't2'; } } class Demo{ use T{ t2 as protected; } public function test(){ return 'test'; } } $demo = new Demo; echo $demo->t1(),'</br>'; echo $demo->t2(),'</br>'; echo $demo->test();
Output result:
The above knowledge introduces some commonly used features. For details, please refer to the official manual:
https ://www.php.net/manual/zh/language.oop5.traits.php
For more PHP related questions, please visit the PHP Chinese website: https:// www.php.cn/
Thank you!
The above is the detailed content of An in-depth analysis of PHP traits. For more information, please follow other related articles on the PHP Chinese website!