Traits in PHP (with Examples)
In this lesson: Learn PHP traits — syntax, multiple traits, conflict resolution and when to use traits vs inheritance — with examples.
The problem traits solve
In PHP a class can extends only one parent. But sometimes two unrelated classes need the same helper methods — say a logging feature. Copy-pasting it everywhere is bad. A trait solves this: define the shared methods once, then pull them into any class with use.
Defining a trait
<?php
trait Logger {
public function log($msg) {
return "LOG: " . $msg;
}
}
?>
A trait looks like a class but uses the trait keyword. You cannot create an object from it — it exists only to be pulled into real classes.
Using a trait
<?php
class Order {
use Logger; // pull in the trait's methods
}
$o = new Order();
echo $o->log("Order placed");
?>
The use Logger; line copies the trait's log() method into Order, as if it were written there. Any class can do the same, without inheritance.
Combining multiple traits
A class can pull in several traits at once.
<?php
trait Logger { public function log($m){ return "LOG: $m"; } }
trait Timestamp { public function now(){ return "2026-01-01"; } }
class Report {
use Logger, Timestamp;
}
$r = new Report();
echo $r->log("done") . " at " . $r->now();
?>
This is where traits shine — you mix in abilities from several sources, something single inheritance cannot do.
Trait vs interface
| Point | Trait | Interface |
|---|---|---|
| Contains code? | Yes (real methods) | No (names only) |
| Purpose | Share an implementation | Enforce methods exist |
| Keyword | use | implements |
Use a trait to share ready code; use an interface to require that certain methods exist. They are often used together.
Common mistakes
- Trying to instantiate a trait. You cannot do
new Logger()— traits go inside classes. - Ignoring method-name conflicts. Two traits with the same method need
insteadof/asto resolve. - Overusing traits. Too many mixed-in methods make a class hard to follow — use them purposefully.
- Confusing traits with interfaces. Traits carry code; interfaces do not.
Summary
- A trait is reusable method code you pull into classes with
use. - It solves PHP's single-inheritance limit for shared code.
- A class can use several traits at once.
- Traits carry real code; interfaces only list method names.
- Resolve same-name method conflicts with
insteadof/as.
Next, enforce method contracts with abstract vs interface, or review classes in PHP OOP.
Frequently Asked Questions
What is a trait in PHP?
use keyword. It is PHP's answer to reusing code across unrelated classes.Why do we need traits if we have inheritance?
What is the difference between a trait and an interface?
Can a class use more than one trait?
use TraitA, TraitB;. Their methods all become available in that class, which is a clean way to mix in different abilities.What happens if two traits have the same method name?
insteadof and as keywords to choose or rename which method to keep. It prevents silent, confusing overrides.