In the "PHP Foundation" This document, saw the trait method, feel more unfamiliar, so this morning with two hours, check out some of the features and usage of the trait method, organized and published this blog.
Trait is a new feature in PHP5.4 and is a solution for PHP multiple inheritance. For example, it would be cumbersome to inherit two Abstract classes at the same time, Trait to solve the problem.
Trait the simple use
Trait need to be defined before use, the definition of trait and the way the class is defined similar
Trait First_trait {
function First_method () {/* Code here */}
function Second_method () {/* Code here */}
}
Also, if you want to use the Trait in Class, you need to use the Using keyword
Class First_class {
Note this line, declaring the use of first_trait
Use first_trait;
}
$obj = new First_class ();//Executing the method from trait
$obj->first_method (); Valid
$obj->second_method (); Valid
We can directly declare the use of a defined trait in a class, and then, after the class is instantiated, you can call the method in trait directly using the method in the calling object. There is no doubt that this adds a lot of extensibility, because the inheritance of classes is single, and the use of trait is not single.
a class the use of multiple Trait
Multiple Trait can be used in the same Class, and multiple Trait are separated by commas.
For example:
Trait first_trait
{
function First_method () {echo "Method1";}
}
Trait Second_trait {
function Second_method () {echo "Method2";}
}
Class First_class {
Now using the more than one trait
Use first_trait, second_trait;
}
$obj = new First_class ();
Valid
$obj->first_method (); Print:method1
Valid
$obj->second_method (); Print:method2
Trait Conflict
Conflicts can inevitably occur between multiple Trait, for example, if two Trait are inserted with a method of the same name, a fatal error will occur if the conflict is not resolved explicitly.
In order to resolve the naming conflicts of multiple trait in the same class, you can use the keyword: insteadof and as;
The insteadof operator is used to explicitly specify which conflict method to use;
The as operator is used to rename and introduce conflicting methods.
Trait First_trait {
function First_function () {
echo "From first Trait";
}
}
Trait Second_trait {
The name here is the same as the first_trait, there will be conflicts
function First_function () {
echo "from Second Trait";
}
}
Class First_class {
Use First_trait, second_trait {
Declare here to replace First_function with first_trait
Declared in the second_trait
First_trait::first_function insteadof second_trait;
Second_trait::first_function as second_trait;
}
}
Trait the abstract method
We can declare an abstract method in trait, so that the Class that uses it must implement it.
Trait First_trait {
function First_method () {echo "Method1";}
Abstract public Function Second_method ();//The modifier can be added here, stating that the calling class must implement it
}
Class First_method {
Use first_trait;
function Second_method () {
/* Code here */
}
}
Trait Priority Level
The PHP manual says: Precedence is the method of the current class that overrides the trait method, and trait overrides the inherited method.
That is, priority: The current class method > Trait Method > Inherited method.
If a method with the same name is defined in a different priority, the high priority method overrides the low priority method;
Two trait are of the same priority, and if a method with the same name is present, a fatal error is reported, see above.
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 result is output:
Hello world!
Trait access Rights Control
Similar to classes, when we define trait methods, we can also specify their access rights (public, protected, private);
We can also use as to modify the access rights of a method.
Trait HelloWorld {
Public Function SayHello () {
Echo ' Hello world! ';
}
}
Modify access control for SayHello
Class MyClass1 {
Use HelloWorld {SayHello as protected;}
}
Give the method an alias that changes the access control
The access control of the original SayHello has not changed
Class MyClass2 {
Use HelloWorld {SayHello as Private Myprivatehello;}
}
Trait between the nesting
When using trait, different trait can be made into a new trait, which makes the code more reusable and, of course, more demanding for the design of the program.
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 ();
Output Hello world!
Trait the static member
Similar to classes, static members are static and static, and both are declared with the static keyword.
Trait Counter {
Public Function Inc () {
static $c = 0;
$c = $c + 1;
echo "$c \ n";
}
}
Trait Staticexample {
public static function DoSomething () {
Return ' Doing something ';
}
}
Trait Properties
When a property is defined in trait, an instance of the class can access it as if it were a property of its own. And can give it to set access permissions, the difference cannot be instantiated ...
It is important to note that if trait defines a property, the class will not be able to define properties of the same name, or an error will result. If the attribute's definition in the class is compatible with the definition in trait (the same visibility and initial value), the error level is e_strict, otherwise it is a fatal error.
Trait Propertiestrait {
public $x = 1;
}
Class Propertiesexample {
Use propertiestrait;
}
$example = new Propertiesexample;
$example->x;
Points needing special attention
These are the basic use of Trait, in more detail you can refer to K Official Handbook.
Here is a summary of the points to note:
-Trait overrides the parent class method that invokes the class inheritance
-Trait cannot be instantiated using new as Class
-Single Trait can be made up of multiple Trait
-In a single Class, multiple Trait can be used
-Trait support modifiers (modifiers), such as final, static, abstract
-We can use the insteadof and as operators to resolve conflicts between Trait
Reference K Literature
-http://php.net/manual/zh/language.oop5.traits.php
-Https://www.doubear.com/article/raityongfaxuexibiji.html
-Http://www.kuqin.com/web/20111119/315048.html
-Http://www.jb51.net/article/61260.htm
At last
Welcome to pay attention to my personal "Hua Weijun", with me to learn to explore PHP, to the technology of the road, Daniel, we walk together!