Skip to content
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

Open
phackwer opened this issue Oct 23, 2017 · 3 comments
Open

"Prefer composition over inheritance": Why you don't use traits? #130

phackwer opened this issue Oct 23, 2017 · 3 comments

Comments

@phackwer
Copy link

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.

@peter-gribanov
Copy link
Contributor

peter-gribanov commented Jul 12, 2019

In example Prefer composition over inheritance EmployeeTaxData plays the role of ValueObject more than extensions of functionality.

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?

@vidic-igor
Copy link

vidic-igor commented Sep 25, 2023

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.

@peter-gribanov
Copy link
Contributor

@vidic-igor ClockAwareTrait is just an example of bad practice in using traits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants