Timestampable is a so-called trait and is meant to help reuse code.
Let's say you have a bunch of entities and all of them have some common functionality, e.g. they all need $createdAt
and $updatedAt
fields. You could achieve this functionality by creating a common base class for all entities, but that wouldn't be great from architecture standpoint.
Instead you could create a trait:
trait Timestampable
{
protected DateTime $createdAt;
protected DateTime $updatedAt;
...
}
And then use it like so:
class User
{
use Timestampable;
}
Now User
objects will have $createdAt
field and any other fields or methods that are defined in the trait, just like they would be defined inside the User
class. This way you don't need to repeat the same code in all of your classes.
Timestampable is one of more common traits, but obviously traits aren't limited to that. Here's an example of a more complete Timestapable trait.
I know this is a 4 year old question, but it was the top result on google, so thought it may be helpful for others.