Use trait in trait php

PHP implements a way to reuse code called Traits.

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

Example #1 Trait example

Precedence

An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.

Example #2 Precedence Order Example

An inherited method from a base class is overridden by the method inserted into MyHelloWorld from the SayWorld Trait. The behavior is the same for methods defined in the MyHelloWorld class. The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class.

The above example will output:

Example #3 Alternate Precedence Order Example

The above example will output:

Multiple Traits

Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas.

Example #4 Multiple Traits Usage

The above example will output:

Conflict Resolution

If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods.

Since this only allows one to exclude methods, the as operator can be used to add an alias to one of the methods. Note the as operator does not rename the method and it does not affect any other method either.

Example #5 Conflict Resolution

In this example, Talker uses the traits A and B. Since A and B have conflicting methods, it defines to use the variant of smallTalk from trait B, and the variant of bigTalk from trait A.

The Aliased_Talker makes use of the as operator to be able to use B's bigTalk implementation under an additional alias talk.

Changing Method Visibility

Using the as syntax, one can also adjust the visibility of the method in the exhibiting class.

Example #6 Changing Method Visibility

Traits Composed from Traits

Just as classes can make use of traits, so can other traits. By using one or more traits in a trait definition, it can be composed partially or entirely of the members defined in those other traits.

Example #7 Traits Composed from Traits

The above example will output:

Abstract Trait Members

Traits support the use of abstract methods in order to impose requirements upon the exhibiting class. Public, protected, and private methods are supported. Prior to PHP 8.0.0, only public and protected abstract methods were supported.

Caution

A concrete class fulfills this requirement by defining a concrete method with the same name; its signature may be different.

Example #8 Express Requirements by Abstract Methods

Static Trait Members

Traits can define static variables, static methods and static properties.

Note:

As of PHP 8.1.0, calling a static method, or accessing a static property directly on a trait is deprecated. Static methods and properties should only be accessed on a class using the trait.

Example #9 Static Variables

Example #10 Static Methods

Example #11 Static Properties

Properties

Traits can also define properties.

Example #12 Defining Properties

If a trait defines a property then a class can not define a property with the same name unless it is compatible [same visibility and initial value], otherwise a fatal error is issued.

Example #13 Conflict Resolution

Safak Ozpinar / safakozpinar at gmail

10 years ago

Unlike inheritance; if a trait has static properties, each class using that trait has independent instances of those properties.

Example using parent class:


Example using trait:

greywire at gmail dot com

10 years ago

The best way to understand what traits are and how to use them is to look at them for what they essentially are:  language assisted copy and paste.

If you can copy and paste the code from one class to another [and we've all done this, even though we try not to because its code duplication] then you have a candidate for a trait.

Stefan W

9 years ago

Note that the "use" operator for traits [inside a class] and the "use" operator for namespaces [outside the class] resolve names differently. "use" for namespaces always sees its arguments as absolute [starting at the global namespace]:



On the other hand, "use" for traits respects the current namespace:



Together with "use" for closures, there are now three different "use" operators. They all mean different things and behave differently.

chris dot rutledge at gmail dot com

10 years ago

It may be worth noting here that the magic constant __CLASS__ becomes even more magical - __CLASS__ will return the name of the class in which the trait is being used.

for example



The magic constant __TRAIT__ will giev you the name of the trait

qeremy [!] gmail

7 years ago

Keep in mind; "final" keyword is useless in traits when directly using them, unlike extending classes / abstract classes.



But this way will finalize trait methods as expected;

t8 at AT pobox dot com

10 years ago

Another difference with traits vs inheritance is that methods defined in traits can access methods and properties of the class they're used in, including private ones.

For example:

rawsrc

4 years ago

About the [Safak Ozpinar / safakozpinar at gmail]'s great note, you can still have the same behavior than inheritance using trait with this approach :

Chủ Đề