This article introduces the content is about "PHP class and object" trait, has a certain reference value, now share to everyone, the need for friends can refer to
Trait (PHP 5.4.0)
Translation trait
Trait is a code reuse mechanism that is prepared for PHP-like single-inheritance languages.
Trait to reduce the limitations of single-inheritance languages, developers are free to reuse method in separate classes within different hierarchies.
Trait is similar to Class, but only designed to combine functionality in a fine-grained and consistent way.
Trait adds a combination of horizontal features to the traditional inheritance; that is, there is no need to inherit between several classes of the application.
Priority Level
Members that inherit from the base class are overwritten by the members that are inserted by trait. Precedence is the method from which the member of the current class overrides the trait, and trait overrides the inherited method.
(When a method or property has the same name, the methods in the current class override the Trait method, and the trait method overrides the method in the base class.) )
Example of #Example #2 precedence <?phpclass Bases {public function SayHello () { echo ' Hello '; }} Trait Sayworld {public function SayHello () { Parent::sayhello (); Echo ' world! '; }} Class Myhelloworld extends Bases { //Public Function SayHello () { // echo ' the class '; } use Sayworld;} $o = new Myhelloworld (); $o->sayhello (); Output: Hello world!? >
Example #3 Another example of priority order <?phptrait HelloWorld {public function SayHello () { echo ' Hello world! '; }} Class Theworldisnotenough {use HelloWorld; Public Function SayHello () { echo ' Hello universe! '; }} $o = new Theworldisnotenough (); $o->sayhello (); Output: Hello universe!? >
Multiple trait
Separated by commas, multiple trait are listed in the use declaration and can be inserted into a class.
Example #4 Multiple Trait usage <?phptrait 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 ();//output:hello World!? >
Naming conflicts
If two trait all insert a method with the same name, a fatal error will result if the conflict is not resolved explicitly.
In order to resolve the naming conflicts of multiple trait in the same class, it is necessary to use the INSTEADOF operator to explicitly specify which of the conflicting methods to use.
The above method only allows the exclusion of other methods, and the as operator can introduce aliases for a method. Note that the AS operator does not rename the method, nor does it affect its methods.
<?phptrait a {public function A () { echo ' A1 '; } Public Function B () { echo ' A2 '; }} Trait B {public function A () { echo ' B1 '; } Public Function B () { echo ' b2 '; }} Class C { Use a, b { b::a insteadof a;//insteadof Specifies the A method in Class B in Class A to resolve the naming conflict b::b insteadof A; A::a as a1;//alias The A method in Class A to A1 to resolve the naming conflict a::b as A2; }} $C = new C (), $C A (),//b1$c-B (),//b2$c-A1 ();//a1$c, A2 ();//a2?>
as
You can also modify the access control of the method
Example #6 Modify the access control of the method <?php trait Hello {public function Traithello () { echo "hello,trait\n"; } } //Modify Traithello access Control class Class1 {use Hello { Traithello as protected; } } Give method an access control alias, the original Traithello access control is not changed class Class2 {use Hello {Hello::traithello as private hi;< c15/>} } $Obj 1 = new Class1 (); $OBJ 1->traithello (); # reported fatal error because the Traithello method was modified to protected $OBJ 2 = new Class2 (); $OBJ 2->traithello (); # The original Traithello method is still public //$OBJ 2->hi (); # reported fatal error because the alias Hi method was modified to private
Trait can also combine Trait
Support for abstract methods, static properties, and static methods in trait
<?phptrait Hello {public function SayHello () { echo "hello\n"; }} Trait World {use Hello; Public Function Sayworld () { echo "world\n"; } Abstract public Function Getworld (); Public Function Inc () { static $c = 0; $c = $c + 1; echo "$c \ n"; } public static function DoSomething () { echo "Doing something\n"; }} Class HelloWorld {use World ; Public Function Getworld () { return ' get World '; }} $OBJ = new HelloWorld (); $Obj->sayhello ();//hello$obj->sayworld ();//worldecho $Obj->getworld (). "\ n";////get Worldhelloworld::d osomething ();//doing something$obj->inc ();//1$obj->inc ();//2
Trait A property is defined, the class cannot define a property of the same name, otherwise it will produce fatal error. There is an exception: the property is compatible (same access visibility, initial default value). Prior to PHP 7.0, the properties were compatible and there would be e_strict reminders.
example #12 resolve conflict <?phptrait propertiestrait {public $same = true; Public $different = false;} Class Propertiesexample {use propertiestrait; Public $same = true; PHP 7.0.0 After the problem, the previous version is e_strict to remind public $different = true; Fatal error}?>