PHP classes can only extend one parent — no multiple inheritance. A trait solves the specific problem that limitation creates: sharing the same set of methods across classes that aren't related by inheritance at all.
trait Loggable {
public function log(string $message): void {
echo '[' . date('Y-m-d H:i:s') . '] ' . $message . PHP_EOL;
}
}A class pulls in a trait's methods with use, as if they were written directly in the class.
class Order {
use Loggable;
public function ship(): void {
$this->log('Order shipped');
}
}
class User {
use Loggable; // same trait, an unrelated class
public function register(): void {
$this->log('User registered');
}
}class Order {
use Loggable, Cacheable, Notifiable;
}| Inheritance (extends) | A trait (use) |
|---|---|
| "Is-a" relationship — a Dog is an Animal | A shared capability across otherwise unrelated classes |
| One parent only | Multiple traits at once |
| Models a real hierarchy | A reusable bundle of behavior, not a hierarchy |
Traits don't force behavior
A trait method can be overridden by the class using it — the class's own version always wins if both define the same method name.