Blog/ Traits

We'll start from where we left off in the previous article on [PHP Interfaces](/blog/drupal-8/interface). If you haven't read it, please go and read it now, or get the free eBook on object-oriented programming from the right sidebar (below the content if you are viewing in mobile). In previous article on PHP Interfaces, we made `HondaAccord` class implement the `ContainerInterface` because it has a trunk. As a result, we could use `HondaAccord` class wherever `ContainerInterface` is used. But the code looks ugly! Since interfaces can't provide implementation, the methods `putItTrunk` and `takeFromTrunk` needed to be implemented in both the classes. This prevents code reuse. That's where traits come to the rescue. Traits help you embed a set of properties and methods in several independent classes that could be living in different hierarchies. Here is the `ContainerTrait` trait created in `ContainerTrait.php` file.

Notice that in both the files, we are using `use ContainerTrait;` to include `ContainerTrait` in each of the classes. Just by using this statement, both `HondaAccord` and `Container` class can now use the properties defined in the `ContainerTrunk` as its own. As an example, add the following code to the end of `Vehicle.php`.

Note that we didn't have to define the methods `putInTrunk()` and `takeFromTrunk()`, and the property `$stuff` in `HondaAccord` class but we could still use these since `HondaAccord` class used `ContainerTrait`. These methods and properties will be available in whichever class we use this trait. This makes for very efficient code reuse. If you followed everything till this point, then you understand traits well enough to be able to follow Drupal 8 code. PHP spec and rules about traits are quite extensive but those are outside the scope of this post. If you want to read them, go to [http://php.net/manual/en/language.oop5.traits.php](http://php.net/manual/en/language.oop5.traits.php). ## When to use traits? Use traits when you want a lot of independent classes to have the same method or property without duplicating code in all these classes. In this post, we saw how using `ContainerTrait` helped us reduce code in `Container` and `HondaAccord` classes. Another example is `ColorTrait`. You could define the property `$color` and methods `getColor()` and `setColor()`. This trait could be used in any class which tries to mimic a real object having color.