 Hello, this is Christian. Welcome to episode 4 of this video series. This time we'll be talking about traits in PHP. So here is the list again for traits. Traits can have a constructor, although you cannot instantiate your object directly. It can have data fields methods, behaves very similar to a class. You cannot extend classes or implement any interfaces, and same rules can have extra methods. So let's go and see how it's done in PHP. So here is my previous demo. I'm going to close this and let's create another class. Let's call it traits. Oops, don't know where that comes from, but okay, so traits. Now a trait, you can declare a trait using the word trait in the name of your trait. For example, I just call it A for now, okay? So trait A is very similar to a class. You can have a constructor and again, just type it underscore underscore and pick the constructor that didn't do it. So you have a constructor. In set constructor, I can echo a statement just to show that it's trait A. Okay, so that is a trait. You cannot declare an object out of that. I can say A is new A. That's not possible. You can see it says cannot instantiate trait. Okay, so that fails. Now let's say I have another one here called trait B. And because trait can use other traits, so you can't really say extends. You cannot use the word extends, okay, A or other class, right? You can't do that or implement. These are some special keywords only apply to class and interfaces. So if you want to include trait A in here, you must use the keyword use inside the class space. So here use A like that. It must be in the class space. It cannot be inside a function. If I have a function called, you know, show, if you want to have a function called show, and it's completely legal, you cannot put that inside here. Okay, it must exist only in the public and the class space. Okay, so now it doesn't matter what you put it before or after. It doesn't really matter. But usually you put up the front because it just makes more sense on the top here. So you can see I have two traits, and one is using the other. I can have more traits if I want to that can use more than one. So let me just kind of collapse this. And if I create another trait here called C, I just empty just to illustrate this, if you want to include traits in here too, you just add it to your list here. You can use just a single line like that separate each with a comma. Or you can use a separate line. It doesn't matter. But usually just do this way. It's just shorter. Okay, so you can have multiple traits and other traits. And of course, each of these can have its own constructor. There's some rule to that as well show you a minute. So that means it can have a constructor here. C can have its own constructor too. So we can go here constructor, it has echo C here. Just I'll just show those two. Okay, and you can see that has an error already it tells you is a trait construct will not be applied because it collides with a. Okay, because a, as you can see, a already has a constructor. So the rule is that when you're using trait in other traits like this, and if these two traits have been used this way, and if one of these has a constructor already, the first one will take precedence. Okay, you cannot use a module constructor here if they are changed like this. Alright, so, so therefore you either have to turn this off and let's see has it or turn C off and let a has have it. Okay, you cannot have both. Just again, some rules. But inside the traits of yeah, it can have its own trait constructor here. Okay, so yeah, that's fine because it's local to that one here. But if you're using other traits, the only those one of those can have the constructor. Okay, and so that's that's the rule. And of course, you can also have variables, I can have variable up here at a class, can make it private data a data is 12, right. And of course, your constructor can, you know, can set that as well, I can go here and say this, behave like a class, use this keyword, show date data is maybe it's now 200. When I instantiate when I created my objects. Okay, so that is how you can use traits. So for example, I cannot instantiate trait, I must have it through a concrete class. So down here, I have to have a class called maybe a trait. That's right, traits. And you cannot extend, you know, you cannot use the word extends or implement traits, you use the same fashion you do here. So if you want to use B, because B uses A and C, so you can use all of them, or you can do the same as you see here. So use A and C, just like that. And then now, if I go and instantiate my trait class, trace class, you can see it's legal now. And then I can access these functions, the show, I can't because it's in the B trait, that doesn't work. But the A has a function work as a constructor. And this initializes the trait data of 12, and then to 200. So if I were to go and then just run this, and then see what happens. So let's go to the terminal here. This time I'm going to go PHP traits. Okay, you see that it shows the message trait A, because it's calling the constructor, right, the constructor for the trait A, and it shows that. And if you want to print the data, it's private, so you can't, right, if you make this public. So we can access it. Okay, so because we are instantiating, we call this constructor, it's going to set the data from 200 to 12. Okay, so if I go down here, and also print, I'm going to dump the A of data, you see that it's 12, right? And let's see what happens. Okay, you see that now is the value is a 200. Okay, it initially was 12, because we invoke the constructor. If I turn the constructor off. Okay, so now this is not being called. So it should be 12. As you can see, it's 12. And then there's this time no constructor were implicitly explicitly created. So it's going to use the global constructor to create this object called traits. All right. And so that is how traits are used. And so again, the rule is you can have other traits using other traits. Just that one must have its own. You can only have one constructor if you are combining or using more than one trait and other traits were in the class. And so that's pretty much about it for this one here. And a next video, I'm going to combine all these, the traits interfaces in class all together to kind of do a little demo to see how they all work together. So if you have any questions, please let me know.