Traits are a mechanism for code reuse, the convention traits in CakePHP are suffixed with Trait so they can be discernible from classes or interfaces. Our book has a basic sample of Trait.
For me, writing code with Traits it's not a regular daily job, but last week I found myself writing a Trait for handling the Theme of an Application.
We need to define the theme on AppView and AppController, and we don't want to repeat ourself:
Beyond re-use code, we can use Traits to separate our code by responsibility.
We like this approach so much that our Users Plugin utilizes it.:
Basically, instead of creating a regular controller with many methods, we create Traits and group the actions by domain business.
Sometimes we need to override the functions, and we want add our custom logic after -before or replace the function on Traits:
If you wanna bake Traits more complexly I recommend you check out the PHP manual… there,you can see how to fix conflicts, change the visibility of methods and so much more.
That's all bakers! I hope you’ve found this information to be helpful!