A PHP trait is a reusable block of methods and properties. You declare it with trait Name { ... } and pull it into any class with use Name; inside the class body. PHP copies the trait's members into the class, so they work exactly as if you had written them there.
User and Robot have no common parent, yet both now have hello(). Traits exist for this case: PHP classes can extend only one class, and a trait shares code sideways, between classes that are not related.
Traits with properties
A trait can bring properties along with its methods. The methods use $this like any method of the class, because once the trait is copied in, they are methods of the class:
A class may not declare a property with the same name as a trait property unless both declarations are identical (same visibility, type and default value); otherwise it is a fatal error.
Use multiple traits
List several traits in one use statement, separated by commas, or write several use lines. The class gets the members of all of them:
Resolve conflicts with insteadof and as
When two traits define a method with the same name, PHP refuses to guess and stops with a fatal error. Inside a use block, insteadof picks the winner, and as keeps the losing method under another name:
With a plain use EnglishGreeting, FrenchGreeting; and no block, the script stops before anything is printed:
PHP Fatal error: Trait method FrenchGreeting::greet has not been applied as Host::greet, because of collision with EnglishGreeting::greet in /home/index.php on line 12
as can also change visibility. use Loggable { log as protected; } makes the trait's log() protected in this class only, and use Loggable { log as private writeLog; } adds a private alias.
Abstract and static members in a trait
A trait can require something from the class that uses it by declaring an abstract method. It can also have static methods and static properties. Each class that uses the trait gets its own copy of a static property:
Car and Bike count separately, because the static property is copied into each class. That is different from a static property declared in a shared parent class, which all children share; the static page shows that case.
Which method wins: class, trait or parent
If the same method name exists in the class itself, in a trait it uses, and in its parent class, PHP picks in this order: the class's own method, then the trait's, then the parent's. Inside the trait, parent:: still reaches the parent's version:
A trait is not a type
instanceof never matches a trait, and a parameter typed with a trait name rejects every object with a TypeError. To ask "does this class have the behavior", pair the trait with an interface, or inspect the class with class_uses():
The pattern above is the usual one: the interface Exportable is what functions type-hint and what instanceof checks, and the trait ExportsProperties is a ready-made implementation that classes can use or replace. A trait on its own is shared code with no contract; when you notice yourself checking class_uses() in business logic, the trait wanted an interface beside it. For code shared by a family of related classes, an abstract class may be the simpler choice.
Frequently Asked Questions
What is a trait in PHP?
A trait is a named group of methods (and properties) declared with trait Name { } that a class pulls in with use Name;. PHP copies the trait's members into the class, so they behave as if you had written them there. It is a way to share code between classes that do not have a common parent.
Can a PHP class use multiple traits?
Yes: use Timestamps, SoftDeletes; or several use lines. If two traits define a method with the same name, you must pick one with insteadof, for example use A, B { A::hello insteadof B; }, and you can keep the other under a new name with B::hello as helloB;.
What is the difference between a trait and an interface in PHP?
An interface is a type with no code: it says which methods a class must have, and instanceof works with it. A trait is code with no type: it pastes method bodies into a class, but $obj instanceof SomeTrait is always false. They are often combined: an interface for the type, a trait with a default implementation.
Can a trait have a constructor or abstract methods?
Yes to both. A trait can declare abstract public function name(): string; to require the using class to provide that method, and it can define __construct, although a constructor in a trait is easy to clash with the class's own and is rarely a good idea.
Which method wins if a class and a trait define the same method?
The class's own method wins over the trait's, and the trait's method wins over a method inherited from the parent class. So the order is: class, then trait, then parent.