Traits in PHP implements code reuse usages

Source: Internet
Author: User
Tags abstract define abstract traits

This article mainly introduces the traits implementation code reuse examples in PHP, this article explained trait simple use, priority problem, multiple trait conflict problem, as can be used to modify the method access control, trait use trait and so on, the need for friends can refer to the

PHP5.4 after the new traits implementation of code reuse mechanism, trait and class similar, but can not be instantiated, do not need to inherit, only in the class using the keyword use to introduce can be introduced into a number of 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 21 22-23 <?php 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 Trait A {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 ext   Ends 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;

Insteadof can be used to identify which approach to use in a conflict;

Use the as operator to name one of the conflicting methods;

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-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 the 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 SayHello access Control class A {use HelloWorld {SayHello as protected;}}   Give the method a change of access control alias//original SayHello access control is not changed class B {use HelloWorld {SayHello as Private Myprivatehello;}} $b = new A (); $b->sayhello (); Fatal Error:call to protected Method A::sayhello () from the context '

(5) Use trait in trait

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-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 can not 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 method} class B {use A; public Function test2 () {}} $b = new B (); $b->test1 (); 1 $b->test1 (); 2

(7) Trait can define attributes, 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.