trait is a collection of methods, used as a "simple conceptual model for structuring object-oriented programs" similar to mixins. Traits provide a simple way to create classes that reuse behavior from software components.” Short version is that they are a collection of methods that can be used by other classes and a class can use many traits. Traits are not mixins, but traits implementation in PHP is more like mixins than traits. But wait, theres more... What?
No instantiation needed • Shares scope (attributes etc.) with including class • Helps reduce code duplication • Can improve cleanliness of code • Can be used to create multiple inheritance scenarios Why would you use traits?
Resolved and ‘flattened’ at compile-time so cannot be replaced at runtime • Trait is therefore highly coupled to class • This in turn makes testing more difficult • Traits cannot be tested as a unit • Class making use of trait cannot be tested in isolation Remember the high coupling?