‫All right.
‫So we're going to dip into object oriented programming.
‫And we're going to get more into this later on when we get into the project.
‫But this is like an introductory.
‫So in this video I want you to understand how to create a class and how to what's called instantiate
‫a new object from that class.
‫Okay.
‫So remember a class is basically a blueprint for an object.
‫So the way that we create it is of course we need our PHP tags.
‫And then we use the class keyword.
‫Now a lot of videos about object oriented programming.
‫They use things like cars and stuff like that, which isn't isn't really you know, it's not a real
‫world situation.
‫Again, if you're making a game, maybe, but I like to use more real world examples.
‫And I think users are a good example because just about every or any decent sized application is going
‫to have users.
‫So let's create a class called user.
‫And when you create a class, the convention is to have the have it uppercase and have it be pascalcase
‫if there's more than one word.
‫So if this was user class, then we would make the C uppercase as well.
‫All right.
‫So this is how we create a class.
‫Now in that class we can have properties.
‫And you can think of a property as an attribute.
‫So maybe we have a name and an email.
‫So I'm going to go ahead and define I'm going to say public.
‫Name and public.
‫Email.
‫So these are the two properties that this this user object can have when we create it from this class.
‫Now I use the public keyword to specify the visibility of the property.
‫And we'll learn more about visibility in I think the next lesson.
‫For now, just know that public means that the property can be accessed from outside of the class.
‫All right.
‫Now you can also set default values to these if we wanted to.
‫So for instance, if I wanted to set this to a string of John Doe and set the email to John at gmail.com
‫so I could do that.
‫I mean, in this case, you probably wouldn't want to do that because then every user you create is
‫going to be named John Doe.
‫So I'm actually going to just get rid of that.
‫But you can do that.
‫And there are situations where you may want to.
‫All right.
‫So we have our properties.
‫Now the next thing I want to show you is how we can add a method.
‫Now a method is just a function that belongs to a class.
‫So methods can also have identifiers.
‫So we're going to just make it public.
‫And then we can just create it like a regular function.
‫Let's say we want a function called login.
‫And I'm just going to do an echo from here.
‫Obviously we don't we're not creating a login system just yet.
‫So let's say the user logged in.
‫All right.
‫That's all this method does.
‫So this is an example of a very, very simple class.
‫Now this by itself doesn't do anything.
‫It's just a blueprint, right?
‫If I save it and I run this, nothing's going to happen.
‫So what I want to show you now is how we can create or what we call instantiate a new user object from
‫this class.
‫So let's come down here and let's say instantiate a new.
‫Object.
‫And I'm just going to put some comments up here.
‫So these are properties.
‫And then we have methods which are again just functions inside of a inside of a class.
‫So to instantiate we use the new keyword.
‫So let's create a variable.
‫Let's just call it user one.
‫And then we're going to say new user with parentheses.
‫Because you can also pass stuff in here which I'll get to in a second.
‫So this will actually instantiate a new object.
‫Now we can access these this these properties and this method outside of the class because we made them
‫public.
‫So technically I could do user one.
‫Name equals John Doe.
‫And let's say user one email equals John at.
‫Gmail.
‫Okay.
‫So I can do that.
‫And then if I want to see what this user looks like we could use var dump.
‫And let's pass in the user one variable.
‫And then if I reload, you'll see we get an object.
‫I know this looks kind of weird I think I have it zoomed in.
‫Yeah.
‫All right.
‫So we have an object user object with a name which is a string of John Doe.
‫And we have an email which is a string of John at gmail.
‫All right.
‫So if we wanted to call this method we could do that as well.
‫So if I come down here and I take my user one object and I call login.
‫And I run that you can see.
‫Actually, let me just comment this far.
‫Dump out.
‫It echoes out the user logged in.
‫So it ran this function.
‫So this is the syntax that we use.
‫Is the arrow not a fat arrow.
‫Not an equal sign but a hyphen.
‫And then the greater than.
‫And I could create as many users as I want.
‫So I could instantiate another one.
‫I could say user two equals a new user.
‫And then I could create user name.
‫Equals Jane Doe.
‫So if I do that and then I do a var dump of user to.
‫Then let's see.
‫Sign.
‫Property.
‫Name.
‫Yeah.
‫Oh, it should be user two.
‫So if I do that, you can see now I have Jane.
‫Jane Doe user.
‫So one thing you're not going to want to do is every time you instantiate a new user is is do this.
‫And a lot of the times these properties will be private, so they won't even be accessible outside of
‫the class.
‫So what you're going to want to do is pass these into the class when you instantiate it.
‫So that's where the construct method or constructor comes in, which is a specifically named method
‫that we create in the class.
‫So let's come in here and we're going to say public.
‫So just like if we were creating any method we say public and then function.
‫And you want to call this double underscore construct okay.
‫Specifically has to be called that.
‫And this will run whenever the the object is instantiated.
‫So just to show you that I'll just say constructor ran.
‫And if I go ahead, let me just get rid of the var dump here.
‫And let's reload.
‫Let's see.
‫What did I do?
‫Forgot my semicolon.
‫So you can see it says constructor ran the user logged in constructor ran.
‫So the reason for that is because when I instantiated this object, that constructor ran.
‫Then I called the login which echoed out the user logged in.
‫Then I instantiated a new user.
‫So the constructor ran again.
‫So every time you instantiate a new object, in this case a user, that constructor is going to run.
‫So typically what you'll do if you're going to pass in specific data such as the name and email is you'd
‫pass that in through the constructor.
‫So we could say name.
‫And email.
‫Okay.
‫And then what we would do is take whatever is passed in and set that to those properties so we can access
‫this name and this email from within any of these methods, not just the constructor, by using the
‫this keyword.
‫So money sign this and then name okay.
‫And the word this the key word this can confuses a lot of people, but it's pretty simple.
‫And once you use it a few times you'll understand how simple it is.
‫And it just means that particular object.
‫Right.
‫So when we're dealing with this user and we say this name, it's going to be John Doe.
‫This name for this user is going to be Jane Doe.
‫So in this case what I'm going to do is set this name equal to whatever the name passed into the constructor.
‫And then we'll do the same with the email.
‫So here let's say this.
‫Email equals email.
‫Now let's come down here.
‫And instead of doing it this way, when we create a new user, we can now pass in the name, let's say
‫John Doe and the email let's say John.
‫At gmail.
‫And then when we create the user down here.
‫We can say.
‫Jane Doe.
‫And for the email we'll just say Jane at gmail.
‫All right.
‫So now if I var dump user to.
‫Reload that you can see we have Jane Doe.
‫So instead of just manually setting the properties and accessing them out here, I just pass the values
‫into the constructor.
‫So they come right there, right here.
‫And then I set it to the name and the email properties of that particular object.
‫And if I wanted to access it through this login right here, instead of saying the user, let's do this,
‫let's say echo this.
‫This name.
‫And then we'll concatenate logged in.
‫So now if I come down here let's do user one.
‫Log in and then underneath that.
‫Let's do.
‫User to log in.
‫And now when we reload, we see John Doe logged in and Jane Doe logged in.
‫So this name right here it pertains to whichever the whichever user we're looking at.
‫Okay, so hopefully that makes sense.
‫It's a little hard to explain, but I think that that's kind of the simplest way to put it.
‫So in the next video I want to talk more about these these visibility or what we call access modifiers.