Earlier today I was doing research on PHP\'s abstract classes, interfaces, and traits.
As far as I can tell, an abstract class says \"anything using me will be using the
When you use traits, you simply require a given class to implement some methods.
You won't inherit any properties or methods and you won't force the object into any inheritance tree.
Thus you can have several completely unrelated classes using the same trait, just to guarantee any object of theses classes will support a given method.
this is how PHP does the famous Mixins. Basically a Mixin is just a class that can share common traits with several other classes. Traits allow to enforce that constraint on the methods of classes, independently of the way these classes inherit from each other or not.
Instead of having to do multiple inheritances to the point of silliness when you want a class to combine the behaviour of two parents, you can use traits to obtain the same result without the hassle of ineriting a bunch of unwanted other things.
It's a bit like saying if floats exist, why do integers exist, they both serve their own specific purposes, but more importantly a look at the history of PHP will shed some light on this:
PHP was initially built without any support at all for classes, and over the years more and more has been added to extend the capabilities of PHP as we push it further and further (and arguable play catch up with other OOP based languages)
2004 - Abstract classes and interfaces introduced
2012 - Traits introduced
That's an 8 year break, in that time among other things the need arose to add support for interfaces with implementations, and so the trait was born
I think there is some philosophical difference on how and when to use them.
You said :
If you focus on your own wordings it makes sense.
Abstract Classes are in reality define things that are abstract e.g Vehicle is an abstract thing until or unless its materialized in the form of a car or a bike . Neither interface define it nor traits.
Interfaces compliment the class inheritance functionality where a class inherits from multiple classes(only certain languages provide multiple inheritance e.g C/C++). Interfaces , as names suggest focus on the INTERFACE , and not the implementation of the interface method in class which is implementing it. It makes classes PLUG & PLAYABLE so everyone should follow a standard. If you further read about factory and adapter pattern on OOP you will understand it.
Traits have implementation/functionality that is not bound to specific classes.Instead it could be found across different classes. Its like a gene in genetics which stays mute in parents and appear only in certain children. Or to be concise selective inheritance but not bound to single class. So it provides a way much better code-reuse
Edit
Interface + Trait != Abstract Class , because when using Trait inheritance is selective
as you select specific trait to use and while using Abstract Class
inheritance is mandatory or dictated by parent class you don't have freedom!