Ever wonder why that trait seemed like a good idea until you needed to test it?
Traits in PHP look convenient at first, but they hide dependencies, break encapsulation, and make your code harder to understand.
I wrote about why I avoid traits and what works better: dependency injection, composition, and simple classes.
Real dependencies should be visible in the constructor, not burried in protected methods.
The rule is simple: if behavior can be extracted into a seperate object, it should be.
dev.to/tegos/why-i-avoid-php-traits-and-what-i-use-instead-1288
Back