PHP Traits Implementation code Reuse example, phptraits_php tutorial

Source: Internet
Author: User
Tags define abstract traits

PHP traits implements code reuse usages, Phptraits


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 use

<?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  ,} $c = new C (); Echo $c->var1;//tes T1$c->test2 (); Trait B::test2 ()

(2) Priority issues
Trait overrides the inherited method, and the current class overrides the trait method.

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 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;

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

Trait HelloWorld {public  function SayHello () {    echo ' Hello world! ';  }}//Modify access control for SayHello class A {  us E HelloWorld {SayHello as protected;}} Give method a change of access control alias//The original SayHello access control is not changed class B {use  HelloWorld {SayHello as Private Myprivatehello;}} $b = n EW A (); $b->sayhello (); Fatal Error:call to protected Method A::sayhello () from context '

(5) Use trait in trait

Trait A {public  function test1 () {    echo ' test1 ',  }} trait B {public  function test2 () {    echo ' test2 ' ;  }} Trait C {use  B;} class D {use  C;} $d = new D (); $d->test2 ();//test2

(6) Trait supports abstract methods, supports static methods, cannot directly define static variables, but static variables can be referenced by trait methods.

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

Trait a {public  $test 1;} class B {use  A;  Public $test 2;}

http://www.bkjia.com/PHPjc/998801.html www.bkjia.com true http://www.bkjia.com/PHPjc/998801.html techarticle PHP Traits Implementation code reuse instance, Phptraits PHP5.4 after the new traits implementation code reuse mechanism, trait and class similar, but can not be instantiated, without inheritance, need only in the class ...

  • 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.