Implementation of code reuse using traits in PHP
This article mainly introduced the PHP traits implementation code reuse use instance, this article explained the trait simple use, the priority question, the multiple trait conflict question, as can use to modify the method access control, trait uses the trait and so on the content, needs the friend can refer
PHP5.4 after the new traits implementation code reuse mechanism, trait and class similar, but can not be instantiated, without inheritance, only need to use the keyword in the class using the introduction can, can introduce multiple traits, with ', ' separated.
(1) Trait simple to use
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 |
Trait A { Public $var 1 = ' test1 '; Public Function test1 () { Echo ' Trait a::test1 () '; } } Trait B { Public $var 2 = ' test2 '; Public Function test2 () { Echo ' Trait b::test2 () '; } } Class C { Use a, B; } $c = new C (); Echo $c->var1; Test1 $c->test2 (); Trait B::test2 () |
(2) Priority issues
Trait overrides the inherited method, and the current class overrides the trait method.
?
| 1 2 3 4 5 6 7 8 9 Ten One + All + + 2 0 * * + / / / |
Trait A { /p> Public $var 1 = ' test '; Public Function test () { echo ' a::test () '; } Public function test1 () { echo ' a::test1 () '; } } Class B { Public function test () { echo ' b::test () '; } Public function test1 () { echo ' b::test1 () '; } } Class C extends b{ Use A; Public Function test () { echo ' c::test () '; } } $c = new C (); $c->test ();//c::test () $c->test1 ();//a::test1 () |
(3) Multiple trait conflict issues
If the conflict is not resolved, a fatal error is generated;
You can use Insteadof to identify which method of the conflict is used;
One of the conflicting methods can be named using the as operator;
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 |
Trait A { Public Function test () { Echo ' a::test () '; } } Trait B { Public Function test () { Echo ' b::test () '; } } Class C { Use a, b { B::test insteadof A; B::test as T; } } $c = new C (); $c->test (); B::test () $c->t (); B::test () can be named as another |
(4) As can be used to modify method access control
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Trait HelloWorld { Public Function SayHello () { Echo ' Hello world! '; } } Modify access control for SayHello Class A { 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 B { Use HelloWorld {SayHello as Private Myprivatehello;} } $b = new A (); $b->sayhello (); Fatal Error:call to protected Method A::sayhello () from context ' |
(5) Using trait in trait
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 |
Trait A { Public Function test1 () { Echo ' test1 '; } } Trait B { Public Function test2 () { Echo ' Test2 '; } } Trait C { Use a, B; } Class D { Use C; } $d = new D (); $d->test2 (); Test2 |
(6) Trait supports abstract methods, supports static methods, and cannot directly define static variables, but static variables can be referenced by trait methods.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Trait A { Public Function test1 () { static $a = 0; $a + +; echo $a; } Abstract public Function test2 (); can define abstract methods } Class B { Use A; Public Function test2 () { } } $b = new B (); $b->test1 (); 1 $b->test1 (); 2 |
(7) Trait can define properties, but the same name property cannot be defined in a class
?
1 2 3 4 5 6 7 8 |
Trait A { Public $test 1; } Class B { Use A; Public $test 2; |
http://www.bkjia.com/PHPjc/1000095.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000095.html techarticle traits implementation of code reuse in PHP this article mainly introduces the traits implementation code reuse example in PHP, this article explains the trait simple use, the priority problem, the multiple TR ...