Home > Article > Backend Development > Simple definition and usage introduction of PHP code reuse traits
Traits is a code reuse mechanism prepared for single inheritance languages like PHP. Traits are designed to reduce the constraints of single-inheritance languages and allow developers to freely reuse method sets in independent classes within different hierarchies. The semantics of traits and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and mixins.
Trait is similar to a class, but is only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal features to traditional inheritance; that is, members of application classes do not need to be inherited.
Trait Example
<?php trait ezcReflectionReturnInfo { function getReturnType() { /*1*/ } function getReturnDescription() { /*2*/ } } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo; /* ... */ } class ezcReflectionFunction extends ReflectionFunction { use ezcReflectionReturnInfo; /* ... */ } ?>
Members inherited from the base class are 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 order example
<?php class Base { public function sayHello() { echo 'Hello '; } } trait SayWorld { public function sayHello() { parent::sayHello(); echo 'World!'; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(); $o->sayHello(); ?>
The above routine will output: Hello World!
Members inherited from the base class are overwritten by the sayHello method in the inserted SayWorld Trait. Its behavior is consistent with the methods defined in the MyHelloWorld class. The order of precedence is that methods in the current class override trait methods, which in turn override methods in the base class.
Another example of priority order
<?php trait HelloWorld { public function sayHello() { echo 'Hello World!'; } } class TheWorldIsNotEnough { use HelloWorld; public function sayHello() { echo 'Hello Universe!'; } } $o = new TheWorldIsNotEnough(); $o->sayHello(); ?>
The above routine will output: Hello Universe!
Multiple traits
separated by commas, in use The declaration lists multiple traits, which can all be inserted into a class.
Examples of usage 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(); ?>
The above routine will output: Hello World!
Conflict resolution
If both traits are inserted A method with the same name will generate a fatal error if the conflict is not explicitly resolved.
In order to resolve the naming conflict of multiple traits in the same class, you need to use the insteadof operator to explicitly specify which of the conflicting methods to use.
The above method only allows to exclude other methods. The as operator can introduce one of the conflicting methods under another name.
Conflict resolution example
<?php trait A { public function smallTalk() { echo 'a'; } public function bigTalk() { echo 'A'; } } trait B { public function smallTalk() { echo 'b'; } public function bigTalk() { echo 'B'; } } class Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; } } class Aliased_Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; B::bigTalk as talk; } } ?>
In this example Talker uses traits A and B. Since A and B have conflicting methods, they define using smallTalk from trait B and bigTalk from trait A.
Aliased_Talker uses the as operator to define talk as the alias of B's bigTalk.
Modify the access control of the method
Using the as syntax can also be used to adjust the access control of the method.
Example of modifying method access control
<?php trait HelloWorld { public function sayHello() { echo 'Hello World!'; } } // 修改 sayHello 的访问控制 class MyClass1 { use HelloWorld { sayHello as protected; } } // 给方法一个改变了访问控制的别名 // 原版 sayHello 的访问控制则没有发生变化 class MyClass2 { use HelloWorld { sayHello as private myPrivateHello; } } ?>
Composing traits from traits
Just as classes can use traits, other traits can also use traits. By using one or more traits when a trait is defined, it can combine some or all members of other traits.
Example of composing trait from trait
<?php trait Hello { public function sayHello() { echo 'Hello '; } } trait World { public function sayWorld() { echo 'World!'; } } trait HelloWorld { use Hello, World; } class MyHelloWorld { use HelloWorld; } $o = new MyHelloWorld(); $o->sayHello(); $o->sayWorld(); ?>
The above routine will output: Hello World!
The above is the detailed content of Simple definition and usage introduction of PHP code reuse traits. For more information, please follow other related articles on the PHP Chinese website!