PHP Reference ยท Functions and Classes
PHP traits
Reference for PHP traits: declaring a trait, use in classes, conflict resolution with insteadof and as, abstract and static members, properties and constants.
A trait is a bundle of methods, properties and (since PHP 8.2) constants that you can paste into several unrelated classes with the use statement. It solves the limits of single inheritance: a class can use many traits, each adding behavior without being part of the class hierarchy.
A trait cannot be instantiated. When a class uses it, the members behave exactly as if they had been written in that class.
trait Greets { public function hello(): string { return "Hello from " . static::class; } } class Robot { use Greets; } echo (new Robot)->hello(); // Hello from Robot
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
trait Name { // properties, methods (including static and abstract), constants (8.2+) } class C { use TraitA, TraitB { TraitA::method insteadof TraitB; TraitB::method as aliasName; method as protected; // change visibility } }
| Construct | Meaning |
|---|---|
use T; |
Copy the trait's members into the class. |
A::m insteadof B; |
Use A's version when both traits define m. |
m as alias; |
Add another name for the method. |
m as protected; |
Change the method's visibility. |
abstract method |
The using class must implement it. |
Precedence: class members override trait members, and trait members override inherited members.
Errors: two traits with the same method name and no insteadof is a fatal error. A property declared in both the class and the trait must be compatible (same visibility, type and default).
Usage notes
- Traits are not types. You cannot type-hint a trait or use
instanceofwith it; pair a trait with an interface to get a type. - Each using class gets its own copy of static properties and static variables, as the
A::bump()andB::bump()example shows. selfandstaticinside a trait refer to the using class;__CLASS__too.__TRAIT__gives the trait's name.- Traits can use other traits.
- Keep traits small and focused (
HasId,Timestamps). Large traits hide dependencies and make classes hard to read. - Constructor and properties: a trait can have a constructor, but conflicts are common; prefer plain methods.
- Testing: test traits through a class that uses them.
- Detect usage with
class_uses()(does not include parent classes' traits) andReflectionClass::getTraits(). - Trait constants (PHP 8.2) cannot be accessed through the trait name directly, only through the using class.
Related
- Interfaces, Abstract classes, Inheritance, static
- Lessons: Traits, OOP design principles