-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Prefer composition over inheritance": Why you don't use traits? #130
Comments
In example Prefer composition over inheritance There are no examples of Traits because there are very few normal cases when Traits are really necessary and have an advantage over other implementations. I know just a few such examples:
These are very specific examples, not everyone understands and not everyone needs. In my opinion, they are too complicated for this guide. Can you give a simple example that will be really useful, and not Trait for the sake of Trait? |
The new Symfony Clock component, has good use case for trait implementation. Instead of having a dependency injection for each service you want to make "clock aware", you can just extend it by using a trait (ClockAwareTrait). also all test that are required to be "time senstive" aka. aware of the clock in order for it to be mocked inside test, you don't need to extend the test case with a base class or write it from scratch each time, you can just use a trait (ClockSensitiveTrait) in order to reuse and set the clock for each test that needs it. |
@vidic-igor ClockAwareTrait is just an example of bad practice in using traits. |
You could really add a good multi-inheritance or composition example by mentioning threads. Although they may force a developer to jump into polimorphic tables, they are a great way to handle this.
I can't find an example of use of trait anywhere on your "clean-code-php" guide.
The text was updated successfully, but these errors were encountered: