What is the Trait?

Since PHP 5.4.0, PHP has implemented a method of code reuse called a Trait.

  • Traits are a code reuse mechanism for single-inherited languages.

  • Traits are similar to classes in that they add horizontal combinations of features to traditional inheritance, eliminating the need for multiple unrelated classes to inherit from each other

  • Traits enable unrelated classes to use the same properties and methods.

Simple to use

<? php trait Test { public function echoHello() { echo 'Hello Trait'; } } class Base { public function index() { echo 'index'; } } class One extends Base { use Test; } class Two extends Base { use Test; } $one = new One(); $two = new Two(); echo $one->echoHello(); echo $one->index(); echo $two->echoHello();Copy the code

The Hello Trait index Hello Trait is displayed.

Members inherited from the base class are overwritten by members inserted into traits. Precedence is the method that members from the current class override, while traits override the inherited method.

<? php trait Test { public function echoHello() { echo 'Hello Trait'; } } class Base { use Test; public function echoHello() { echo 'Hello Base'; } } class One extends Base { use Test; public function echoHello() { echo 'Hello One'; } } class Two extends Base { use Test; } $one = new One(); $two = new Two(); $base = new Base(); echo $one->echoHello(); echo $two->echoHello(); echo $base->echoHello();Copy the code

Hello One Hello Trait Hello Base is displayed.

  • The Class One example overrides base classes and Trait tests to show that the methods of the current class take precedence over them.

  • The Class Two example overrides the base class, and traits take precedence over inherited base classes.

  • The Class Base example overrides the Trait Test to show that the methods of the current class take precedence over the Trait.

The use declaration lists multiple traits, separated by commas, that can all be inserted into a class.

<? php trait Test { public function echoHello() { echo 'Hello '; } } trait TestTwo { public function echoWord() { echo 'word ! '; } } class One { use Test,TestTwo; } $one = new One(); echo $one->echoHello(); echo $one->echoWord();Copy the code

The result is Hello Word! .

If both traits insert a method with the same name, a fatal error will result if the conflict is not explicitly resolved.

<? php trait Test { public function echoHello() { echo 'Hello Test'; } public function echoWord() { echo 'word Test'; } } trait TestTwo { public function echoHello() { echo 'Hello TestTwo '; } public function echoWord() { echo 'word TestTwo'; } } class One { use Test, TestTwo { Test::echoHello as echoTest; Test::echoWord insteadof TestTwo; TestTwo::echoHello insteadof Test; } } $one = new One(); echo $one->echoTest(); echo $one->echoWord(); echo $one->echoHello();Copy the code

Hello Test word Test Hello TestTwo.

  • Use as as as an alias, that is, Test::echoHello as echoTest; Output echoHello in the Trait Test.

  • The insteadof operator is used to exclude other traits, namely Test::echoWord insteadof TestTwo; The output is word Test, using echoWord from the Trait Test

Modify the control permission of a method

<? php trait Test { public function echoHello() { echo 'Hello'; } public function echoWord() { echo 'word'; } } trait TestTwo { public function echoHello() { echo 'Hello TestTwo '; } public function echoWord() { echo 'word TestTwo'; } } class One { use Test { echoHello as private; } } class Two { use Test { echoHello as private echoTwo; } } $one = new One(); $two = new Two(); echo $two->echoHello();Copy the code
  • The output is Hello.

  • If echoHello is made private using AS in Class One, it cannot be accessed through Class One.

  • In Class Two, you rename it using AS, then make the new naming method private, and the methods in the original Trait are accessible as normal.

Attributes can also be defined in traits like classes. Is very easy to use!