PHP Tutorial (& MySQL) #41 - Classes & Objects (part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
or rather than ganks so we've seen a lot of basic datatypes in PHP so far we've seen integers floats strings arrays etc but what if we wanted to create our own custom data type or object with certain properties and functions that we can use with that object for example we could have a user data type which would represent a user on our website perhaps and that user would then have access to certain properties an email a username a gender maybe and it would also have access to several functions specific to that data type like a login function or a logout function so how would we create such a data type our own type of data well first of all we create what's known as a class and a class is like a blueprint for an object a blueprint for a data type it describes what properties and what functions an object of that type will have and then what we could do is create an object based on that class based on that blueprint and this is called instantiating a class so let's do this let's first of all create a class and we do that by using the classic keyword then we give the class a name I'm going to call this user and we open up our braces now class names typically start with a capital letter right there now inside the class we need to define what properties and what functions these user types are going to have now we're going to have an email property and also a name property so how do we define those well we don't just say email is equal to something a name is equal to something first of all we have to say what type of property is this is this a public one or is this a private property now public means we can access the property and change them from anywhere outside the class in our code and private means we can only access them from inside the class itself now typically what we should do with our properties is set them to private so that our objects are a little more protected then if we want to change the value of one of our properties on the object we use a class function to do that now that might have gone poof over your head for now so what we're gonna do to begin with is set these to public and then we'll talk about setting them to private later on and seeing how that works so public remember it means we can access these different things these different properties outside of the class so we'll create a public property first of all called email and we're not going to set it equal to a value yet we'll just create that property on this class and we'll also create one called name so right now this class has an email property and a name property now we also want to create a function and this function is also going to be public and we'll call this function login because users can log in and inside this function what we'll do is just echo the user logged in so something simple right so we have this class now which describes a kind of type of object now what we'd like to do is create a new object based on this class so how do we do that well the way we do it is by first of all creating a variable to store this object in and we'll call it user one and set that equal to a new user this is called instantiating a class we're creating a new object and we're storing it inside this variable and that object is going to be based on the user class so we use the new keyword first of all then the class name and we invoke that and it creates a new object now this user one is going to have an email property and name property they won't have values but they'll have those properties and also it's going to have access to a login function so then how do we access those different properties and those different functions well the way we do it we'll start with the function is by saying user one which is the variable we stored it in then an arrow which is a - or a - and a carrot like that then we say whatever function or property we want to use and we're going to use the login function and we have to invoke it using the parentheses much like or any other function so that is gonna then fire this function because you remember when we instantiate a class we're creating a new object based on that blueprint on that class and now that object has these properties and this function so what accessing that function from that new object hope that makes sense and we should see this echoed to the browser so if I save this now and preview over here then we see the user logged in awesome now if we try to echo the name of the email this won't work I'll show you that I'm gonna say user one arrow and we want the name now we don't have to use dollar sign right here we just say whatever the variable is called so name or email all right so it knows we're trying to access this all this okay so if we try now to echo this and we have to say echo in front of it because this is not a function that echoes anything out like this we're echoing of value here only it doesn't have a value yet so let's save it and see what happens refresh and we don't see anything it's not work because this doesn't have a value it only has an empty property if you like so then how do we actually set these properties well the best way to do it is by using a constructor function a constructor function is a special type of function inside classes that runs whenever we instantiate a class so whenever we say new user it's going to run that constructor function and that constructor function can then set some initial values for our properties so the way we do this is by first of all same public and then function and the special name is underscore underscore construct like so that is the name it must be so this is a function and this function can then access these different values right here and set them equal to something now the way we do that is by saying dollar sign this and that refers to this object that we're creating okay so it refers to kind of like the class itself when we're creating a new user it creates a new use based on this class and then this refers to that new user so when we say this and then arrow email it's referring to the email property much like we said user one name it would be like saying this name okay and we're going to set that equal to an initial value so I could set that equal to I don't know Mario that's the net ninja Cody at UK so that's going to be the initial email value of any object created with this class okay to begin with and I'm going to do the same for the name so this and then arrow name is equal to Mario okay so now when we're creating a new user over here then it's going to actually have the name Mario so if we save this it should echo out Mario now let me save it and refresh over here and now we see Mario right there so that worked and we could change this to email if we wanted to to get the email instead refresh and now we see the email right there perfect now this is flawed because every time we create a new user we're creating a user with the same initial values for the email and the name now when we create new users they're not all going to have the same email and the same name that's just weird so instead what we'd like to do is be able to set those initial values ourselves so let me now just comment this stuff out and instead what I'd like to do is create a new different user so I'll call this user two and set this equal to new user so we're creating a new user again okay and this time what I'd like to do is maybe pass in an email and a name so I can do that we can take in those values here as parameters to this constructor function and also supply them here when we're creating a new user so say for example I want to take in as the first parameter right here and name value and I'm expecting that when we create a new user and the second value I'm going to expect will be an email so we're creating those local balls here now we need to pass those values in so the first argument the first value is the name and I'm going to set that equal to yoshiya and the second one is gonna be Yoshi the net uninjured code at UK okay so we now have a name and an email that we're passing into this constructor when we're creating a new user now we can access those values inside this constructor so what I'm going to do is copy this and paste it down below then I'll comment this stuff out and then what I'll do is instead of hard-coding the email to be this I'm going to set it equal to the email that we receive into this function and likewise for this I'll set it equal to the name that we receive okay now if I now try to echo user two and I want the name and also I'm gonna do echo user two and the email and we don't want that gap right there that was a mistake and if we save this now and preview it then we should see both of those values so we see Yoshi first of all that's the name that we print out or echo out and then we get Yoshi at the net ninja call so that works we're setting those values initially when we create this object and that's awesome because every time we create a new user now we can pass in our own values for that user now we can also access these values that have been set now inside the function so instead of echoing this out we could echo something like instead echo and then we want to output the name source a this and then arrow name so it's accessing the name of this person of this user and then we'll concatenate that with a little string that says logged in so now if I try to log in let me comment those out I'm gonna say user two and then login like so now it's gonna print hopefully out Yoshi logged in so if I save this and refresh over here we can see Yoshi logged in awesome so there we go that's our very first simple class with a couple of different properties and a constructor function and also our own function as well now I did say before that typically when we set our properties over here we don't want them to be public we want them to be private so that they can't just be updated really merely on the fly anywhere and we'll talk about that in the next video
Info
Channel: The Net Ninja
Views: 29,641
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, class, object, php class, php classes, classes, objects, php object, php objects, oop, php oop, class syntax, class vs object
Id: bt4znmLQCZ8
Channel Id: undefined
Length: 11min 28sec (688 seconds)
Published: Thu Mar 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.