Source: matthiasnoback.nl

When to use a trait?
If you want to reuse some code between multiple classes, using a trait is an alternative for extending the class. In that case the trait may be the better option because it doesn't become part of the type hierarchy, i.e. a class that uses a trait isn't "an instance of that trait".

This is the code for that trait, and if you ever attended one of my workshops, you'll know it already: trait EventRecording { /** * @var list */ private array $events = []; private function recordThat(object $event): void { $this->events[] = $event; } /** * @return list */ public function releaseEvents(): array { $events = $this->events; $this->events = []; return $events; }}

We could fix this issue by extracting the code into an object, (adding even more evidence to the statement that there's always an alternative for using a trait): final class EventRecording { /** * @var list */ private array $events = []; private function recordThat(object $event): void { //... } /** * @return list */ public function releaseEvents(): array { //... }}

We could again introduce a trait for that: trait ReleaseEvents { private EventRecording $eventRecording = new EventRecording(); /** * @return list */ public function releaseEvents(): array { return $this->eventRecording->releaseEvents(); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners