WEBVTT

00:00.980 --> 00:05.240
Okay, so in this video we're going to learn about the traits.

00:05.420 --> 00:10.580
So traits is just a simple way to add methods to classes.

00:10.670 --> 00:18.470
Without those methods without those classes having to inherit from other classes.

00:19.580 --> 00:20.780
So let's have an example.

00:20.780 --> 00:23.150
Let's create a user class.

00:26.150 --> 00:27.860
So this is something simple.

00:27.860 --> 00:39.050
Let's call it a model a database model that contains some information and is trying to model a database

00:39.050 --> 00:39.950
table.

00:40.580 --> 00:41.660
It is super simple.

00:41.660 --> 00:43.520
It just contains a name.

00:43.520 --> 00:47.480
So we use constructor property promotion in here.

00:48.320 --> 00:53.090
And it will have just one method called save.

00:53.090 --> 00:55.730
So it saves itself.

00:56.570 --> 00:59.450
It's void doesn't return any value.

00:59.450 --> 01:02.900
And we're not going to implement anything here yet.

01:02.900 --> 01:07.820
I'd like to log the information that something has happened.

01:07.940 --> 01:15.540
Specifically I'd like to log that this name was saved.

01:18.450 --> 01:25.980
Okay, so, um, as you can see, this class user does not have this log method.

01:26.010 --> 01:27.510
Now what's logging?

01:27.540 --> 01:35.280
Well, it's just a way of saying what happened in your app so that you can later review those logs to

01:35.310 --> 01:39.120
figure out what has happened in case anything went wrong.

01:40.020 --> 01:46.080
So to actually use a logger inside the user class, we've got a couple of options.

01:46.650 --> 01:50.940
We can use composition something we've seen previously.

01:51.120 --> 01:59.430
We can pass some kind of a logger class that might actually implement a logger interface, so that we

01:59.430 --> 02:02.310
can easily switch between loggers.

02:02.850 --> 02:03.990
So this would work.

02:03.990 --> 02:10.920
We could pass an object through a constructor and then do something like this logger log.

02:11.550 --> 02:13.350
This would be perfectly fine.

02:13.770 --> 02:18.120
Now another way would be to just use a trait.

02:18.160 --> 02:28.360
Now this user class can still implement an interface, but if it would implement an interface that could

02:28.360 --> 02:34.420
be called logger and it can have a function.

02:36.820 --> 02:38.050
Called log.

02:40.060 --> 02:51.280
That accepts a string message and does not return anything if user would implement this interface.

02:51.310 --> 02:56.830
Well, we would have to actually implement this method every single time.

02:56.830 --> 03:00.730
And this is especially not convenient.

03:00.730 --> 03:09.160
If you would be logging something from multiple different classes, then you'd have to implement the

03:09.160 --> 03:13.690
same log method every single time.

03:14.470 --> 03:18.520
So to the rescue we can use a trait.

03:18.550 --> 03:24.160
Now, as said previously, let's create this trait called Loggable.

03:24.190 --> 03:32.410
Trait is just a set of methods that you can easily add to a class in other programming languages.

03:32.440 --> 03:37.480
That's sometimes called a mixin, or also can be called a partial.

03:39.250 --> 03:40.570
So here I can do.

03:40.600 --> 03:46.660
Public function log string message void.

03:48.100 --> 03:58.810
And in PHP we can just do echo saying logging and just output the message and a new line character like

03:58.810 --> 03:59.470
that.

03:59.500 --> 04:05.290
And now let's run this so we can see the error.

04:05.320 --> 04:07.390
Let's run trade speech P.

04:07.840 --> 04:16.120
So the class user contains an abstract method and must therefore be declared abstract or implement the

04:16.120 --> 04:16.570
method.

04:16.570 --> 04:21.550
We are talking about the log method from this interface.

04:22.180 --> 04:30.130
So now having this trait I can reuse it in every single class by just adding this use.

04:31.960 --> 04:32.620
How is it called?

04:32.620 --> 04:34.090
It's called Loggable.

04:35.200 --> 04:36.010
And voila!

04:36.050 --> 04:44.240
Now our user class will have this method defined so we can create an instance of it.

04:44.240 --> 04:48.500
Right now let's create a user that's new user.

04:51.110 --> 04:51.530
Built.

04:51.530 --> 04:56.060
And let's do user save.

04:58.220 --> 04:59.330
And there it is.

04:59.810 --> 05:01.850
We've got the correct message.

05:01.850 --> 05:09.020
Now just to be clear, traits don't necessarily have to be used with an interface.

05:09.830 --> 05:18.140
I think it's just cleaner if you do it this way, because then it's clear that this class requires to

05:18.140 --> 05:25.430
have some kind of methods and trait is there to provide them, but it is perfectly fine to just use

05:25.430 --> 05:31.850
traits like that, and everything still works, and there is actually nothing wrong about that.

05:31.850 --> 05:39.200
So if we are talking about PHP frameworks, I think that Laravel is using traits a lot.

05:39.200 --> 05:45.770
So if you are interested specifically in Laravel, it's pretty useful to understand traits.
