Okay, so in this video we're going to learn about the traits.
So traits is just a simple way to add methods to classes.
Without those methods without those classes having to inherit from other classes.
So let's have an example.
Let's create a user class.
So this is something simple.
Let's call it a model a database model that contains some information and is trying to model a database
table.
It is super simple.
It just contains a name.
So we use constructor property promotion in here.
And it will have just one method called save.
So it saves itself.
It's void doesn't return any value.
And we're not going to implement anything here yet.
I'd like to log the information that something has happened.
Specifically I'd like to log that this name was saved.
Okay, so, um, as you can see, this class user does not have this log method.
Now what's logging?
Well, it's just a way of saying what happened in your app so that you can later review those logs to
figure out what has happened in case anything went wrong.
So to actually use a logger inside the user class, we've got a couple of options.
We can use composition something we've seen previously.
We can pass some kind of a logger class that might actually implement a logger interface, so that we
can easily switch between loggers.
So this would work.
We could pass an object through a constructor and then do something like this logger log.
This would be perfectly fine.
Now another way would be to just use a trait.
Now this user class can still implement an interface, but if it would implement an interface that could
be called logger and it can have a function.
Called log.
That accepts a string message and does not return anything if user would implement this interface.
Well, we would have to actually implement this method every single time.
And this is especially not convenient.
If you would be logging something from multiple different classes, then you'd have to implement the
same log method every single time.
So to the rescue we can use a trait.
Now, as said previously, let's create this trait called Loggable.
Trait is just a set of methods that you can easily add to a class in other programming languages.
That's sometimes called a mixin, or also can be called a partial.
So here I can do.
Public function log string message void.
And in PHP we can just do echo saying logging and just output the message and a new line character like
that.
And now let's run this so we can see the error.
Let's run trade speech P.
So the class user contains an abstract method and must therefore be declared abstract or implement the
method.
We are talking about the log method from this interface.
So now having this trait I can reuse it in every single class by just adding this use.
How is it called?
It's called Loggable.
And voila!
Now our user class will have this method defined so we can create an instance of it.
Right now let's create a user that's new user.
Built.
And let's do user save.
And there it is.
We've got the correct message.
Now just to be clear, traits don't necessarily have to be used with an interface.
I think it's just cleaner if you do it this way, because then it's clear that this class requires to
have some kind of methods and trait is there to provide them, but it is perfectly fine to just use
traits like that, and everything still works, and there is actually nothing wrong about that.
So if we are talking about PHP frameworks, I think that Laravel is using traits a lot.
So if you are interested specifically in Laravel, it's pretty useful to understand traits.