What Are Traits in PHP?
A trait is a mechanism for reusing methods across multiple independent classes, working around PHP's restriction of single inheritance for regular classes.
Published November 22, 2026
A trait is a reusable group of methods and properties you can include in several unrelated classes with use, working around PHP's single inheritance. It copies the code into the class.
The concept
Traits are not types: you cannot instantiate them or type-hint against them. They just share code between classes.
When two traits define the same method you must resolve the conflict with insteadof and as.
Mix code into classes
Practical examples
Define and use
trait Loggable {
public function log(string $m): void { echo '[' . static::class . '] ' . $m . PHP_EOL; }
}
class Order { use Loggable; }
(new Order)->log('created');Resolve conflicts
class A { use T1, T2 { T1::hello insteadof T2; T2::hello as hello2; } }Laravel example
class Post extends Model { use SoftDeletes, HasFactory; }Common mistakes
- Using traits as a substitute for good design (hidden dependencies).
- Trait method name clashes.
- Traits that depend on properties the class may not have.
Key takeaways
- Code reuse across unrelated classes.
- Not a type.
- Resolve conflicts explicitly.
FAQ
Trait vs interface?
Interfaces define a contract; traits provide implementation.
Trait vs inheritance?
Traits add behaviour without an is-a relationship.