: This article mainly introduces the new Traits feature of PHP learning. For more information about PHP tutorials, see. When I read the source code of yii2, I got started with trait. I learned how to write a blog record.
Since PHP 5.4.0, PHP has implemented a method for code reuse, called traits.
Traits is a code reuse mechanism prepared for a single inheritance language similar to PHP. Trait allows developers to freely reuse method sets in independent classes in different hierarchies to reduce the restrictions of a single inheritance language. The semantics of Traits and class combination defines a way to reduce complexity and avoid typical problems related to traditional multi-inheritance and mixed classes (Mixin.
Trait is similar to a class, but it is only intended to combine functions in a fine-grained and consistent manner. Trait cannot be instantiated by itself. It adds a combination of horizontal features for traditional inheritance; that is, members of the application class do not need to inherit.
Trait example
Priority
The member inherited from the base class is overwritten by the member inserted by trait. The priority is that the members of the current class overwrite the trait method, while the trait method overwrites the inherited method.
Priority example
sayHello();?>
The above routine will output: Hello World!
The member inherited from the base class is overwritten by the sayHello method in the inserted SayWorld Trait. Its behavior is the same as that defined in the MyHelloWorld class. The priority is that the methods in the current class will overwrite the trait method, and the trait method overwrites the methods in the base class.
Example of another priority order
sayHello();?>
The above routine will output: Hello Universe!
Multiple trait
Separated by commas (,). Multiple trait entries are listed in the use declaration and can be inserted into a class.
Examples of multiple trait usage
sayHello();$o->sayWorld();$o->sayExclamationMark();?>
The above routine will output: Hello World!
Conflict resolution
If both trait inserts a method with the same name, a fatal error will occur if the conflict is not explicitly resolved.
To resolve naming conflicts between multiple trait nodes in the same class, you must useInsteadofOperator to specify which of the conflicting methods is used.
Only other methods can be excluded from the preceding method,AsOperators can introduce one conflicting method with another name.
Example of conflict resolution
In this example, Talker uses trait A and B. Because A conflicts with B, it defines the use of smallTalk in trait B and bigTalk in trait.
Aliased_Talker usedAsOperator to defineTalkB's bigTalk alias.
Access control for modification methods
UseAsThe syntax can also be used to adjust the access control of methods.
Example of modifying method access control
From trait to trait
Just as the class can use trait, other trait can also use trait. When trait is defined, one or more trait can be used to combine some or all members of other trait.
Example of trait composition from trait
sayHello();$o->sayWorld();?>
The above routine will output: Hello World!
Abstract member of Trait
To enforce requirements on the classes used, trait supports the use of abstract methods.
An example of a forcible requirement using an abstract method
getWorld(); } abstract public function getWorld();}class MyHelloWorld { private $world; use Hello; public function getWorld() { return $this->world; } public function setWorld($val) { $this->world = $val; }}?>
Trait static member
Traits can be defined by static methods of static members.
Static variable example
inc(); // echo 1$p = new C2(); $p->inc(); // echo 1?>
Static method example
Static variables and static methods
Attribute
Trait can also define attributes.
Example of defining attributes
x;?>
If trait defines an attribute, the class cannot define the attribute with the same name. Otherwise, an error occurs. If the definition of this attribute in the class is compatible with the definition in trait (the same visibility and initial value), the error level isE_STRICT
Otherwise, it is a fatal error.
Conflicting examples
Different Use
Examples of different use
The first use is the use Foo \ Test for namespace, and \ Foo \ Test is found. The second use is a trait and \ Foo \ Bar \ Foo \ Test is found.
_ CLASS _ and _ TRAIT __
_ CLASS _ returned use trait class name ,__ TRAIT _ returned trait name
Example:
testMethod();//Class: BaseClass//Trait: TestTrait
Trait Singleton
The instance is as follows:
name = 'foo'; }}class bar { use singleton; private function __construct() { $this->name = 'bar'; }}$foo = foo::getInstance();echo $foo->name;$bar = bar::getInstance();echo $bar->name;
Call the trait method
Although not obvious, if the Trait method can be defined as a static method in a common class, it can be called.
The instance is as follows:
The above introduces the new features of Traits, a new feature of PHP, including some content, and hope to help those who are interested in PHP tutorials.