I’ve always looked for a rational explanation of using traits in PHP and I think I’ve found one. Let’s say we have a bunch of status codes which are set using constants. To remind: constants can also be set in interfaces (notice that constant interface pattern is considered as a bad practice).
We are writing an utils library and simply don’t trust users :-) It means we need to check if status codes set on object are valid. One way is to keep status codes as a range from number to number. Validating is easy in that case. We’re simply check if status is in range. This is obviously not common. We could use reflection to list all class constants. And what if we have unrelated hierarchies of objects? We must duplicate some code. It’s against DRY principle. Trait to the rescue!
Take a look at my rich documented gist and see how it works at the bottom of listing. Code is licensed under MIT License, so grab it and use it.