Home >Backend Development >PHP Tutorial >PHP implements a code reuse method called trait, reuse trait_PHP tutorial
Since PHP 5.4.0, PHP has implemented a A method of code reuse is called a trait.
Trait is a code reuse mechanism prepared for single inheritance languages like 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.
Trait is a solution to PHP multiple inheritance. For example, it would be very troublesome to inherit two Abstract Classes at the same time. Trait is designed to solve this problem.
It adds a combination of horizontal features to traditional inheritance.
Example 1: Define trait using trait keyword
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>return</span> 'hello'<span>; } }</span>
Example 2: When using traits in Class, use the use keyword. When using multiple traits, separate them with commas
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>return</span> 'hello'<span>; } } trait second_trait{ </span><span>public</span> <span>function</span><span> world(){ </span><span>return</span> 'world'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span> first_trait,<span>second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>->world();
Example 3: Priority
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.
Example: Members inherited from the base class will be overridden by members inserted by the trait
<span>class</span><span> Base { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello '<span>; } } trait SayWorld { </span><span>public</span> <span>function</span><span> sayHello() { parent</span>::<span>sayHello(); </span><span>echo</span> 'World!'<span>; } } </span><span>class</span> MyHelloWorld <span>extends</span><span> Base { </span><span>use</span><span> SayWorld; } </span><span>$o</span> = <span>new</span><span> MyHelloWorld(); </span><span>$o</span>-><span>sayHello(); </span><span>//</span><span>输出的结果</span> Hello World!
Example: The members of the current class override the trait method
<span>trait HelloWorld { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello World!'<span>; } } </span><span>class</span><span> TheWorldIsNotEnough { </span><span>use</span><span> HelloWorld; </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello Universe!'<span>; } } </span><span>$o</span> = <span>new</span><span> TheWorldIsNotEnough(); </span><span>$o</span>-><span>sayHello(); </span><span>//</span><span>输出的结果</span> Hello Universe!
Example 4: Nesting between traits
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>echo</span> 'hello'<span>; } } trait second_trait{ </span><span>//</span><span>trait之间的嵌套</span> <span>use</span><span> first_trait; </span><span>public</span> <span>function</span><span> world(){ </span><span>echo</span> 'world'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span><span> second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>->world();
Example 5: You can declare abstract methods in traits, and the Class or trait using it must implement the abstract method
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>echo</span> 'hello'<span>; } </span><span>//</span><span>抽象方法</span> <span>public</span> <span>abstract</span> <span>function</span><span> test(); } trait second_trait{ </span><span>//</span><span>trait之间的嵌套</span> <span>use</span><span> first_trait; </span><span>public</span> <span>function</span><span> world(){ </span><span>echo</span> 'world'<span>; } </span><span>//</span><span>实现first_trait 中的test方法</span> <span>public</span> <span>function</span><span> test(){ </span><span>echo</span> '!'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span><span> second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>-><span>world(); </span><span>echo</span> <span>$obj</span>-><span>test(); </span><span>//</span><span>会输出</span> helloworld!
Example 6: Conflict resolution
If two traits insert a method with the same name, a fatal error will occur 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, which is equivalent to the alias of the method.
<span>trait A { </span><span>public</span> <span>function</span><span> smallTalk() { </span><span>echo</span> 'a'<span>; } </span><span>public</span> <span>function</span><span> bigTalk() { </span><span>echo</span> 'A'<span>; } } trait B { </span><span>public</span> <span>function</span><span> smallTalk() { </span><span>echo</span> 'b'<span>; } </span><span>public</span> <span>function</span><span> bigTalk() { </span><span>echo</span> 'B'<span>; } } </span><span>class</span><span> Talker { </span><span>use</span> A,<span> B { B</span>::smallTalk insteadof A; <span>//</span><span>trait B 的smallTalk方法会代替 trait A 的smallTalk方法</span> A::bigTalk insteadof B; <span>//</span><span>trait A 的bigTalk方法会代替 trait B 的bigTalk方法</span> <span> } } </span><span>class</span><span> Aliased_Talker { </span><span>use</span> A,<span> B { B</span>::smallTalk insteadof A;<span>//</span><span>trait B 的smallTalk方法会代替 trait A 的smallTalk方法</span> A::bigTalk insteadof B;<span>//</span><span>trait A 的bigTalk方法会代替 trait B 的bigTalk方法</span> B::bigTalk <span>as</span> talk; <span>//</span><span>使用 as 操作符来定义了 talk方法 来作为 B 的 bigTalk方法 的别名</span> <span> } } </span><span>$obj</span>=<span>new</span><span> Talker(); </span><span>$obj</span>-><span>smallTalk(); </span><span>$obj</span>-><span>bigTalk(); </span><span>//</span><span>结果会输出 bA</span> <span>$obj2</span>=<span>new</span><span> Aliased_Talker(); </span><span>$obj2</span>->talk();<span>//</span><span>会输出B</span>
Example 7: Modify method access control
<span>trait HelloWorld { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello World!'<span>; } } </span><span>//</span><span> 修改 sayHello 的访问控制</span> <span>class</span><span> MyClass1 { </span><span>use</span> HelloWorld { sayHello <span>as</span> <span>protected</span><span>; } } </span><span>//</span><span> 给方法一个改变了访问控制的别名 // 原版 sayHello 的访问控制则没有发生变化</span> <span>class</span><span> MyClass2 { </span><span>use</span> HelloWorld { sayHello <span>as</span> <span>private</span><span> myPrivateHello; } }</span>
Example 8: Trait can also define attributes
<span>trait PropertiesTrait { </span><span>public</span> <span>$x</span> = 1<span>; } </span><span>class</span><span> PropertiesExample { </span><span>use</span><span> PropertiesTrait; } </span><span>$example</span> = <span>new</span><span> PropertiesExample; </span><span>$example</span>->x;
If the trait defines a property, the class cannot define a property with the same name, otherwise an error will be generated. If the property's definition in the class is compatible with its definition in the trait (same visibility and initial value) then the error level is E_STRICT
, otherwise it is a fatal error.
<span>trait PropertiesTrait { </span><span>public</span> <span>$same</span> = <span>true</span><span>; </span><span>public</span> <span>$different</span> = <span>false</span><span>; } </span><span>class</span><span> PropertiesExample { </span><span>use</span><span> PropertiesTrait; </span><span>public</span> <span>$same</span> = <span>true</span>; <span>//</span><span> Strict Standards</span> <span>public</span> <span>$different</span> = <span>true</span>; <span>//</span><span> 致命错误</span> }
If you have learned something from reading this article, please give me a thumbs up . If there are any errors in the article, please point it out.
Learn from each other and make progress together!