ZF4 Blog
On the question of interfaces and traits
It often occurs to me that we are not describing Interfaces and Traits properly. How many times do you see them named as SomethingTrait or SomethingInterface.
Now, I admit, it is sometimes difficult, but if we take the following into consideration then it might make life easier.
Interfaces
Interfaces state the "intention" of a class to implement some functionality. This
means the implementing class is able
to do something or capable
of doing something.
Does it not make sense then to name Interfaces accordingly:
- Writeable
- StorageCapable
- CapableOfMakingCoffee
- AbleToMakeTea
Traits
Traits are tricky. A quirk of PHP to enable some sort of multi inheritance. I've seen
whole classes implemented as a single use
statement. Not what was intended, I'm sure.
The real power of Traits is coupling them to Interfaces. They provide the implemention
of an Interface. But how do we name them? It seems to me that doing
it is apt. The
dictionary describes:
doing the activities in which a particular person engages
The key bit of doing
is ing
(non english languages may vary!). Thus we can get
- Writing
- Storing
- CoffeeMaking
- TeaMaking
Putting it all together:
class Foo implements CapableOfMakingCoffee { use CoffeeMaking; }
Your thoughts?