When should i use trait in php?
Show Since PHP version 5.4.0 the language has had the keyword — trait SomeTrait PHP has the function class_uses(SomeClass::class) Use casesThe shared code can be placed into a Trait to prevent duplicates of the code and use a Trait easily in your classes. Laravel uses this technique everywhere :) Using Traits in Laravel makes it easy to get your model to work with a full-text search engine simply by adding the line Laravel Nova uses the But be careful a Trait can’t be “de-use” (disabled). If you need to have a class that uses a Trait and sometime you’ll decide to use this class somewhere without Trait
just decompose it into parts using inheritance and an abstract class. For example, you have the abstract AbstractProduct The propertiesAs I said, the trait can contain more than methods, it also can contain a definition of properties: trait SomeTrait The Resolve naming conflictsIf you have similar names of the methods in the Trait and class and you want to call the method from the Trait in the class, you can resolve it by creating alias using the keyword For example, the Trait and the class have the method trait SomeTrait The output of the above code: doSomething from trait I hope after reading the article you know more than you knew before about the power of Traits in PHP. When to use a trait? Never. Well, a trait could be considered to have a few benefits: Benefits
DownsidesOn the other hand, there are several problems with traits. For instance:
Better alternativesAnyway, in practice, I always find better alternatives for using a trait. As an example, here are some alternatives:
A counter-exampleEven though most situation don't really need a trait, and there are better alternatives, there is one situation that I keep using a trait for. This is the code for that trait, and if you ever attended one of my workshops, you'll know it already:
I use this trait in entities, because there I always want to do the same thing: record any number of events, then after saving the modified state of the entity, release those events in order to dispatch them. One concern might be that
Another concern is that this trait suffers from the lack of "trait-private". As an example, instead of using 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):
We then need to assign an instance of this new class to a private property of the entity:
Every entity would still need that public method
At this point I feel like the extracted Request for TraitsBased on my experience with traits (having seen examples of them in projects, frameworks, and libraries), I don't know of any good case for using traits, so my rule is to never use them. But, you probably have some good examples of traits that make sense, and only make sense as a trait. So here is my Request for Traits. Please share your examples by posting a comment! When should I use traits?Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
Is PHP trait good?PHP Traits are Bad
On the surface, there is strong support for PHP traits because using them can help reduce code duplication throughout your application. In addition, they can help improve maintainability and the cleanliness of your code.
Why do we need traits?Traits can define both static members and static methods. It helps developers to reuse methods freely in several independent classes in different class hierarchies. Traits reduces the complexity, and avoids problems associated with multiple inheritance and Mixins.
When did PHP add traits?Traits was introduced in PHP 5.4 and it's so cool and easy to use. Traits is much more similar to a class but it is only for grouping methods in a fine-grained and consistent way. You can't instantiate a trait like you would do a class, in fact, It is not possible to instantiate a Trait on its own.
|