[0:00] so welcome to a crash course in [0:02] object-oriented PHP for beginners this [0:04] is going to be a video that just kind of [0:06] gets people into optic during the PHP in [0:08] a very easy and smooth way so you can [0:10] learn what and how to use object [0:13] oriented PHP together with your PHP [0:16] applications in the same sense as you [0:18] would using procedural code so this is [0:20] going to be a very beginner friendly [0:21] introduction to Optical into PHP and the [0:25] main focus here is to not just explain [0:27] how to program object or into PHP but [0:30] also explain exactly why we do the [0:33] different things to do inside optic or [0:34] interphp because the why is just as [0:36] important as how when you get started [0:39] otherwise you don't really connect the [0:40] dots and you don't really see okay so I [0:43] know how to create classes and objects [0:45] but how can we use this instead of [0:48] procedural PHP and that's really the big [0:50] question here that I often get people [0:52] asking because one of the hardest things [0:54] for beginners is to see the connection [0:55] and see how we can Implement Optical [0:58] into PHP instead better procedural PHP [1:01] so that is what I'm going to focus on in [1:03] this video here so I'm going to do a [1:04] crash course we're going to talk about [1:05] what classes are how to create Optics [1:07] and we will even do a small example [1:09] we'll show you how to do something that [1:12] you will know how to do using procedural [1:13] PHP but do it instead using object or [1:17] into PHP so let's go and get started in [1:19] a very easy and for you to understand [1:21] where if you've never seen object or [1:23] enter PHP before so as you can see in [1:25] front of me here I have a very basic [1:27] index page there's nothing specific [1:29] inside of it it's just a pure HTML index [1:32] page or it's a index.php file but we [1:35] don't have any PHP inside the page [1:37] itself so if I were to go inside this [1:39] page and just do something that you have [1:40] seen before which is just procedural PHP [1:43] and where to go inside the body tags [1:45] here and open up my PHP tags and we can [1:47] go inside these PHP tags we might be [1:49] able to create a variable we could call [1:51] this something like brand then we can [1:54] set it equal to a car brand in this case [1:56] it would say Volvo then we can close it [1:58] off here we can copy it down to to next [2:00] line and we can change the second one to [2:02] something like color then we say color [2:04] is going to be equal to green and let's [2:07] also go and create a function because a [2:09] function is a very basic thing you know [2:11] how to create functions by now because [2:12] that is just basic procedural PHP so if [2:16] we were to go down and actually create a [2:17] function and now that I'm typing this I [2:20] realize that I haven't really talked [2:21] about what is procedural PHP and what is [2:23] object oriented PHP because some [2:25] beginners may not know that there is [2:27] something called procedural and object [2:29] oriented PHP I mean you do know there's [2:31] something called object oriented [2:33] otherwise you haven't clicked this video [2:34] but what we're creating right now here [2:36] is something called procedural PHP [2:37] because we just procedurally generated [2:39] or created as we need to use it inside [2:42] our web page so right now I need to have [2:44] these variables here and I need to add [2:46] this function here so I'm just going to [2:47] go and create them here since I need to [2:49] use them here so in this case let's go [2:52] and call this function something like [2:53] get car info now I would need to pass in [2:57] the variables inside my function because [2:59] we need to do that because that the [3:00] basics of functions so if I need to use [3:02] these I need to go inside and pass it in [3:04] as a parameter so just going to go ahead [3:07] and pass them in and then inside the [3:08] function we could say something like [3:10] return brand and then we return the [3:13] brand and then we say color and then [3:15] return the color and then we basically [3:16] when we output this inside our website [3:18] so it would actually go ahead and Echo [3:20] out this function down here so we can [3:21] say Echo get car info we're going to [3:25] pass in our parameters so we can [3:26] actually save Brands and then we can say [3:29] color [3:31] and if I were to Output this inside my [3:33] website you'll notice that we get a very [3:35] basic sentence we get brand Volvo color [3:38] green right this is basic PHP that [3:41] you've learned up until now but this is [3:44] what we call procedural PHP so let's go [3:46] ahead and take this example here and [3:48] talk a bit about how we can create [3:49] object oriented PHP programming in order [3:52] to get the same effects but using [3:54] classes and objects instead so the first [3:57] thing we're going to do is we're going [3:58] to go inside our directory and we're [3:59] going to create a new folder and this [4:01] folder is going to be called classes now [4:04] just know that this is something we do [4:06] for the sake of this exercise here [4:07] because you need to know about how to [4:09] create classes and objects and this kind [4:11] of thing but typically we would have [4:13] namespaces inside our directory we would [4:16] actually have different categories where [4:18] we could categorize our classes inside [4:20] of them so we have things a little bit [4:22] more systemized but for now let's just [4:24] go and create a classes folder which are [4:26] going to have classes inside of them so [4:28] we're going to go inside this folder and [4:29] create a new file I'm going to call this [4:31] one on car dot PHP do note my spelling [4:35] here because I did actually capitalize [4:36] the C because this is going to contain a [4:38] class called car and the naming [4:40] convention for classes is to capitalize [4:43] the first letter and in the same sense [4:44] when it comes to the file we do the same [4:46] thing so I'm going to say car with a [4:48] capitalize C and then I'm going to say [4:50] we create this file here inside the file [4:53] itself we're going to open up the PSP [4:54] tag since we do actually to have PHP [4:56] code in here and I'm not going to close [4:58] the PHP tag because again if you know [5:00] basic PHP when you have a pure PHP file [5:03] it is better not to have the closing tag [5:05] otherwise you may get unexpected errors [5:07] so again if you do have questions about [5:09] this I do have a procedural PHP course [5:12] for beginners that you can watch where I [5:14] cover what this is and why we do it this [5:16] way but inside of here we're going to [5:19] learn how to create a class now to [5:22] explain exactly what a class is a class [5:25] is basically a template or blueprint you [5:28] could call it so let's take a real life [5:29] example let's say we have a Factory that [5:32] produces cars and inside this Factory we [5:34] have a machine that has a blueprint of [5:37] how a car is supposed to be built so as [5:40] long as we have this blueprint we can [5:42] print out as many cars as we want based [5:45] on that blueprint and inside the [5:46] blueprint we might have a couple of [5:48] empty spaces where for example the color [5:50] is something that we can decide so we [5:53] don't have to have all the cars being [5:55] one specific color but we can actually [5:57] change that if you want to but we [5:59] essentially had this basic blueprint and [6:01] that is what a class is so let's say I [6:03] go in here and I create a class and we [6:05] do that by defining a class going in [6:08] giving it a name in this case here it's [6:10] going to be car which is the same name [6:11] as my file over here again notice the [6:14] capsize C then I'm going to go after and [6:16] write curly brackets and this is how we [6:18] can create a basic class now inside a [6:21] class we have something called [6:23] properties and methods we do also have [6:25] something called Fields but PHP is a [6:27] little bit different when it comes to [6:28] naming different things inside a class [6:30] compared to maybe be something like C [6:32] sharp which is another programming [6:33] language but just know that we have [6:35] something called a property and a method [6:37] inside a class and I'm going to piss off [6:40] some of the more experienced developers [6:42] now because I'm going to tell the [6:44] beginners here that you should see a [6:46] property and a method in the same way as [6:48] you do as a variable and a function [6:50] which is not true by the way they're not [6:53] the same thing but it's a very similar [6:55] comparison that I can make for people to [6:57] understand what exactly a property and a [6:59] method are so a property is the same [7:01] thing as a variable and a method is the [7:04] same thing as a function so now a [7:05] property is basically information about [7:08] a particular object so right now we're [7:11] trying to make a car right because we [7:13] have a class named car so information [7:15] about a car could for example be the [7:18] brand the color the vehicle type you [7:20] know is this a car or is it a truck what [7:22] is it so just basic information about [7:24] this particular object in here but now a [7:27] method just like a function is when we [7:29] want the card to do so something so we [7:32] wanted to print out something inside the [7:33] website or do something specific you [7:36] know we have functions that can do [7:37] things inside a website and that is kind [7:39] of the same thing as what methods are [7:41] but you know a method is just functions [7:43] inside a object that are related to this [7:46] particular object so with this in mind [7:48] that's actually going to create some [7:49] properties inside this class here now in [7:52] order to do that we have to talk about [7:54] something called visibility modifiers a [7:57] visibility modifier is essentially a way [7:59] for us to go inside a class and then say [8:01] who has access to this information so [8:04] who has access to the properties and the [8:06] methods because inside object or into [8:08] programming we have something called [8:09] encapsulation which basically means that [8:11] we take code and we encapsulate it [8:14] inside for example a class so only [8:16] certain things have access to that [8:17] particular piece of code so this would [8:20] be a better control over what exactly is [8:22] happening to the code who has access to [8:24] it you know is there some sort of code [8:25] that is a little bit more sensitive than [8:27] others well okay then maybe some [8:29] particular classes should not have [8:31] access to this code so in this sort of [8:33] way we can encapsulate code using object [8:35] oriented programming so in order to talk [8:37] about this we need to talk about these [8:39] visibility modifiers so basically let's [8:41] go and create a property here just to [8:43] begin with I am going to do the same [8:45] thing as we did inside the index page [8:47] where we created a brand we created a [8:49] color and then later on we're going to [8:51] go ahead and create this function but as [8:53] a method inside that particular class we [8:55] just created so I'm going to take these [8:57] two variables here I'm just going to [8:58] copy it go inside my class and paste [9:01] them in and immediately you can see some [9:02] error messages and that's because we [9:04] haven't actually created these [9:05] visibility modifiers yet that we will [9:08] create in just a second but basically [9:10] what we have to do here is we need to [9:11] actually make sure that we defined who [9:14] has access to this information here so [9:16] right now I could say that this is going [9:18] to be private so if it's private it [9:21] means that only this class called car [9:23] has access to this information here so [9:26] it would actually copy this down and say [9:27] this is also going to be private and [9:30] save it you can see oh no more error [9:31] messages because now we have created [9:34] properties in the proper way but now [9:36] let's say I want to make sure that not [9:38] just this class here but maybe also [9:40] another class that is a child class of [9:43] this class here has access to these [9:45] properties as well well what you can do [9:47] is you can create instead of a private [9:49] property if you can create something [9:51] called a protected property which now [9:54] means that not just this class here but [9:57] another child class that extends to this [9:59] class has access to this property as [10:02] well and you might already be thinking [10:04] well Daniel you're skipping ahead a [10:05] little bit here what is a child class [10:08] and what does extending to this class [10:10] mean we'll get to that okay so just [10:13] don't worry about it right now just know [10:15] that we do have a protection level or a [10:17] visibility modifier called protected [10:19] that means that other classes that [10:21] extend to this class has access to this [10:24] information so any class that is not a [10:26] child class of this class do not have [10:28] access to this information that would be [10:30] the same way as just putting private [10:31] okay but then we do also have something [10:33] called public which means that everyone [10:37] has access to this information here but [10:39] now that is also kind of where we run [10:41] into a small issue that I see happen [10:43] quite often when people they create [10:45] classes and objects because the thing is [10:48] the reason we have classes is to [10:50] encapsulate data and one of the big [10:53] benefits of encapsulating data inside a [10:55] class is that we say well okay so we [10:58] have information inside this class and [11:01] what we want to do here is we want to [11:02] make sure that we only have access to [11:04] this information if we have a reason to [11:07] and one of the things that I see people [11:09] they do as a default is to go inside [11:11] their class and all the properties [11:13] inside their class they're just going to [11:15] make everything public and that way oh [11:17] everything can be accessed and hey we [11:19] can just use all this information [11:21] anywhere inside our code hooray right [11:23] well that is not what you're supposed to [11:25] be doing that is a bad habit of people [11:28] to have what you should be doing is go [11:30] in and and make everything private as a [11:34] default and then from here you need to [11:36] ask yourself okay inside this particular [11:39] code is there a reason for anything [11:42] outside this class to have access to [11:44] these pieces of information because if [11:46] there isn't a reason then don't make it [11:48] public if you don't fully understand [11:50] private protected public yet don't worry [11:52] we will get to do examples where you'll [11:54] you know have this Epiphany moment where [11:56] you say oh okay so that's why we need to [11:59] make it private and protect it in public [12:01] so we will get to it for now just know [12:03] that the only thing that can access [12:04] these different pieces of information or [12:07] these properties here is this particular [12:09] class here so the card class that we [12:11] have right now I do also want to point [12:13] another thing out here which is that [12:14] right now you can actually see that I [12:16] assigned some data to these different [12:18] properties so the thing is that [12:19] typically when we have these different [12:21] properties inside a class we don't [12:22] actually have data assigned to it [12:24] because this has to be flowy if I want [12:26] to create a car I want to be able to [12:28] decide what kind of brand and which [12:29] color it's going to be when I create the [12:32] object and if I were to set it equal to [12:34] Volvo and green inside the property [12:36] itself now it is actually going to be [12:38] predetermined and I don't want to have [12:40] that so we're just going to go ahead and [12:42] say that we have a brand and we have a [12:43] color but we don't know exactly what [12:45] those information are going to be yet so [12:47] that is going to be for later once we do [12:49] actually create a object based off this [12:52] class here if you do have some sort of [12:53] information that you do want to have [12:55] predefined inside a class you can do so [12:57] so let's go and create a third property [12:59] in this case here we could have [13:01] something called vehicle type so vehicle [13:03] type if that is how you spell it and I'm [13:06] just going to go ahead and set it equal [13:07] to car just so we know that this is [13:10] going to be a car type and just one more [13:12] thing before we move on from properties [13:14] I do want to mention that these are also [13:16] called Fields so you know right now we [13:18] would actually call these properties but [13:21] we do also have another word called [13:23] feels so if we can say forward slash [13:25] fields and this is something that some [13:28] people may ask about because in other [13:29] programming languages for example simple [13:31] like in C sharp we do have something [13:33] called feels and then we do have [13:34] something else that are called [13:36] properties which we'll also talk about [13:38] in this video here which is called [13:39] getter and Setter methods but in other [13:42] programming languages we do actually [13:43] call those properties and these would [13:45] then be called Fields but in PHP these [13:49] are referred to as properties weirdly [13:51] enough so it's not exactly the same [13:53] thing as some other programmer languages [13:55] so just know that in PHP these are [13:58] called properties so now let's go and [13:59] talk about a Constructor so let's go and [14:01] make a comment here and say we have [14:03] something called a Constructor now a [14:06] Constructor is basically a method inside [14:08] this class so basically a function that [14:12] is going to run whenever you create a [14:14] object based off this class here so [14:16] again remember this class is a blueprint [14:18] so when I want to create a car based off [14:21] this blueprint here we run this function [14:24] here in order to run some code to do [14:26] something when the optic is created and [14:29] this is where leaving these m t comes in [14:31] handy because what we can do is I can go [14:34] down here and I can create a public [14:37] function and I'm going to call this one [14:39] underscore underscore so two of them [14:42] construct [14:44] parentheses and curly brackets and this [14:47] is how we create a basic Constructor and [14:49] it is important to note here that this [14:50] is a predefined word inside PHP to [14:53] create a function that is going to run [14:55] when you create an object so this is one [14:57] of the predefined functions inside PHP [14:59] when it comes to object oriented PHP [15:02] that you do need to know okay so this [15:04] one method here is very important to [15:06] know about so what a Constructor [15:07] basically does is that it makes sure [15:09] that whenever we create an object from [15:11] this blueprint here we can go ahead and [15:13] assign some data to the brand and to the [15:16] color that right now has no data [15:18] assigned to them so just like when we go [15:20] inside our index page and we create a [15:22] function and we pass in some parameters [15:24] in order to fill in whatever information [15:26] is going to be output inside the browser [15:28] from inside the function here we can [15:30] also pass in this information inside an [15:33] object in the exact same way so let's go [15:35] and go back to the car and say that [15:37] whenever I want to create an optic based [15:39] off this class here I want to be able to [15:41] decide which brand and which color it's [15:43] going to be which means that I'm going [15:45] to grab this property or at least the [15:47] name of it I'm going to pass it inside [15:48] the parentheses down here comma and then [15:51] the color [15:52] gonna paste it inside the parentheses [15:54] and then inside the function we want to [15:57] actually take the data that we pass into [15:59] the object when we create it and assign [16:01] it to these properties up here so it's [16:04] very important to point out here because [16:05] this confuses a lot of people these over [16:08] here are not these up here okay they [16:11] just have the same names in the same way [16:14] as you go back inside the index page and [16:16] say that okay so we have these [16:17] parameters here these parameters are [16:20] just placeholders okay so the [16:22] information that we have up here are [16:24] going to get passed in when we actually [16:26] call upon the function down here so this [16:28] variable and this variable is going [16:30] inside the function when we actually [16:31] call upon the function the parameters up [16:34] here are just placeholders and that is [16:36] the same thing when we go inside a class [16:38] and we have these different parameters [16:40] inside a Constructor so we're going to [16:42] go inside the method down here and I'm [16:44] going to use a keyword called variable [16:46] this which points to this object here or [16:49] this instance of an object then I'm [16:51] going to point to you a property which [16:54] is right now called brand so right now [16:57] this particular line of code here is [16:59] actually how we refer to a property that [17:01] is called brand which means that I can [17:04] now set this one equal to a piece of [17:06] data that I pass inside the object when [17:09] we created for the first time so I can [17:11] set it equal to variable Brands and [17:14] again just to mention it one more time [17:15] for people might still be confused about [17:17] this this variable brand over here is [17:20] not this variable brand up here okay [17:22] this is the property inside this class [17:25] this down here is the data that we pass [17:28] into the object when we created for the [17:30] first time which is going to be this [17:32] data up here so we take this data and [17:34] then we put it equal to the property [17:37] inside this class here which is this [17:40] brand up here so what we can do now is [17:42] we can say well I do also have a color [17:44] property that right now does not have [17:45] anything assigned to it so what we can [17:48] also do when we create this optic for [17:49] the first time is to also go down and [17:52] say I want to to assign some data to [17:54] that particular property so we're going [17:56] to pass in a color when we create this [17:58] object and then I want to assign it to [18:00] my color property up here okay so now at [18:04] this point here let's actually go and [18:05] create a object based off this class [18:07] here because I do think it's going to [18:09] make a little bit more sense to you if [18:10] we do actually create an object to show [18:12] exactly what the Constructor does so [18:15] what I can do is I can go down here [18:17] let's just go down a couple of lines and [18:19] I'm going to create a variable which I [18:21] can call something like car maybe we [18:23] could call it car number one just to [18:25] give it some sort of name and I'm going [18:27] to set it equal to a new instance of [18:30] this class called car [18:32] parentheses and semicolon so now right [18:35] now you can see oh it's giving me a [18:37] error message and that is because right [18:39] now we told it that when we create an [18:41] object we need to pass in two pieces of [18:43] data that is going to get assigned to [18:45] our properties up here which means that [18:47] we need to go in and say I want to pass [18:49] in Volvo [18:51] and I want to pass in a color so it [18:53] could be for example green and in this [18:56] case that we now don't have any error [18:57] messages because now we actually paste [18:59] it in some information that is going to [19:02] get assigned to our brand and color [19:04] properties inside the class here okay so [19:08] this is how we can actually create an [19:09] object and what I can then do is I can [19:11] say well this is a blueprint so I can [19:13] create a second car we can call this one [19:15] car number two and this one could for [19:17] example be a BMW [19:20] and it is going to be a blue BMW so in [19:24] this sort of way we now have different [19:26] copies of this car based of this [19:29] blueprint but now just to show one extra [19:31] thing here that might be important to [19:33] some people let's say I don't want to [19:34] decide on the color for the second car [19:37] down here now it is going to give me a [19:39] error message because we need to include [19:41] the color otherwise we're not doing this [19:43] correctly right what we can actually do [19:45] inside the Constructor up here is you [19:47] could actually assign some data which is [19:49] going to be a placeholder which is going [19:52] to get filled in if you do not submit [19:54] some data okay so I could for example [19:56] say here that we have none set for the [19:59] color which in this case is going to [20:01] actually remove the error message down [20:03] here because now when I create car [20:05] number two it is going to be a BMW but [20:08] it is not going to have a color assigned [20:10] because now the default is going to be [20:12] none if we do not assign a color the [20:15] first card though is still going to be [20:17] green because this equal sign is only [20:19] going to count if we we do not include [20:21] some data inside the Constructor and in [20:23] this sort of way we can just go and [20:25] create as many cars as we want we call [20:27] this three four five six and we can just [20:30] give some different information if you [20:32] want to this one could for example be a [20:34] Toyota and then we can pass in another [20:37] color this could be yellow just to give [20:40] it some sort of information here but you [20:42] kind of get the idea now that we can [20:43] just keep creating copies on this class [20:46] here because we now have a blueprint and [20:50] when we do actually have a optic so let [20:52] me go ahead and delete this so now [20:53] because we have this object here we can [20:55] actually access any sort of properties [20:57] and methods inside this class or inside [21:00] this object here as long as it has a [21:02] proper protection level so right now we [21:04] can't actually access these properties [21:06] up here because they're all set to [21:07] private but let's go ahead and set this [21:09] bottom one to public just to show an [21:11] example here what I can now do is I can [21:14] actually take this object that we [21:15] created go down to the next line and [21:18] from here we can point to any sort of [21:19] property or method third from inside the [21:21] class that we can actually access so [21:23] right now we can actually access our [21:25] vehicle type because it is now set to [21:27] public so if we were to go down here I [21:29] can use this little arrow again that you [21:31] may recognize from up here because we're [21:32] now pointing to a property called get [21:35] vehicle type so we're going to paste [21:38] that in down here we now refer to the [21:40] property inside this object here called [21:42] vehicle type so if I were to actually [21:44] Echo this out inside my browser so we [21:46] can actually see this if we were to go [21:47] inside my browser here go inside my [21:49] directory and say I want to go inside [21:51] classes forward slash car [21:54] dot PHP then you can see we get car and [21:58] why do we get car because that is what I [21:59] set my property to I can also if I were [22:02] to make this brand into a public [22:04] protection level I can also access this [22:07] one so I could for example go down here [22:09] instead of vehicle type we could say [22:11] brand so in this sort of way we can [22:13] access properties and methods inside the [22:15] class and just to show how this looks [22:17] like if I were to actually make these [22:18] private again so if I were to go in here [22:19] and say I want to make these privates [22:21] and I also want to make this one private [22:23] down here you can now see that well [22:25] first of all you can see we get an error [22:27] message but if I were to go inside the [22:28] browser and refresh you can see that oh [22:30] uncaught error cannot access private [22:32] property because it is private so we're [22:35] not allowed to access it from inside [22:36] this object here so now we talked about [22:38] how to create a object based off a class [22:41] that we created so we have this class [22:42] here we have different properties and we [22:44] also have a Constructor that assign data [22:46] to our properties now one thing you [22:49] might be thinking is well okay so how [22:53] can we use this object that we just [22:54] created what is what is the purpose of [22:56] creating these objects here well the [22:58] purpose of an object is to be able to [23:00] access any sort of properties [23:02] or methods that we have inside a class [23:05] or inside an object because the object [23:07] has all of these properties and methods [23:09] inside of it this means that I'm [23:11] actually able to access Properties or I [23:14] can go down and create a method on my [23:16] own so I can actually go down below this [23:18] Constructor here and I can say well [23:20] let's go and create a method so I'm [23:22] going to say we have a method and I'm [23:24] going to say we have a public [23:27] function and I'm going to call this the [23:29] exact same thing as we did inside the [23:31] index page so if we were to go back in [23:32] here we have this little function here [23:34] so I can actually go and grab this and I [23:36] can paste it inside the class as a [23:38] method and we don't actually need to [23:39] pass in any sort of parameters here [23:41] because we do already have these [23:43] properties up here and these we can [23:45] actually just access because they're [23:47] inside this same class so if I were to [23:49] go down here and open up the curly [23:51] brackets I can go back inside my index [23:53] page and just copy what I have inside [23:55] this function here because we're going [23:57] to do the exact same thing and paste it [23:59] in and instead of referring to these [24:01] different parameters that we would [24:02] typically inside a function pass inside [24:05] to parentheses instead I can actually go [24:07] and refer to this property so I can go [24:10] inside and say this property called [24:12] Brands is going to get referred to and [24:15] then I'm going to do the same thing when [24:16] it comes to the color so instead we're [24:18] going to go down and paste in the [24:20] property called color so this is how you [24:22] would access a property from inside the [24:24] class because remember we did actually [24:26] set these properties to private which [24:28] means that we cannot access these [24:30] properties unless it from within the [24:32] same class so because this method we [24:34] created down here is inside the same [24:36] class this method can access this [24:39] property up here because it is private [24:41] and because this method is set to public [24:43] it means that after we create a object [24:46] based off this class here I can actually [24:48] go down below and I could for example [24:50] Echo variable car number one because [24:53] that is the name of the objects and I'm [24:55] going to point to a method which right [24:58] now is called get car info parentheses [25:02] and we can run these properties or [25:04] methods in the same way as we would with [25:06] variables and functions inside PHP [25:09] things start to make sense now we could [25:12] of course also go up here and if I [25:14] wanted to actually access one of these [25:15] properties directly from outside the [25:18] actual class I could actually go in and [25:20] say that maybe this particular property [25:22] is going to be public because let's say [25:24] I have a reason to make it public [25:26] because I need to use it outside the [25:28] class itself what I could do is I can go [25:30] down here and do that and I could go [25:32] down below just copy paste and instead [25:35] of saying I want to access this method [25:36] here I could just say I want to access a [25:39] vehicle type [25:41] so I'm going to paste that in instead [25:42] and that is how we would access a [25:44] property so now we know how to access a [25:46] property and a method from inside this [25:49] class so again always set your [25:51] protection level to private and then [25:53] change it to public if needed so if we [25:55] need to access this method from outside [25:57] the class then we need to set it to [25:59] public and the same thing goes for [26:01] properties now let's actually go ahead [26:02] and include a break in between here [26:04] because I want to actually show you this [26:05] inside the browser so we're just going [26:07] to Echo out a HTML break because we can [26:09] do that and then I'm going to go inside [26:11] my browser and I'm just simply going to [26:13] go inside my directory code classes [26:15] because we do have that inside our [26:17] website here so classes and I'm going to [26:20] access [26:20] card.php like so and then you can see we [26:23] get the same thing but we also get a car [26:25] below it because we also Echo out the [26:28] property but now typically we don't [26:30] really do this from within the same [26:31] document so right now you know we have [26:33] this class document and we create a [26:35] object inside the same page and that's [26:37] not typically how we do things instead [26:40] what we would actually do is go inside [26:42] for example our index page or any other [26:44] page inside our website and instead of [26:47] doing all of this with functions which [26:48] is completely unnecessary right now we [26:51] could actually go ahead and delete [26:52] everything and we could go ahead and [26:54] load in one of our classes so we could [26:56] say require underscore once in the same [26:59] way as you would include any other [27:00] document and we could go in and say that [27:02] I want to access classes forward slash [27:05] and then card.php [27:07] which means that now we have access to [27:10] this class so I can go below here and [27:12] say I want to create a new object so we [27:14] can call this on car and we can say car [27:16] number one and I'm going to set it equal [27:17] to a new instantiated object based off [27:21] the class called car [27:23] parentheses semicolon and then we just [27:26] paste in the parameters that is required [27:27] so in this case here you know we could [27:29] say this is going to be a I'm not really [27:31] a car guys I'm just going to use BMW [27:33] again [27:34] and we're going to go ahead and say it's [27:36] going to have a color which is going to [27:38] be set to green and then we can actually [27:40] go back inside car and just delete the [27:42] class object that we have down here [27:44] because we don't need it inside the same [27:46] page it was just to give you an example [27:48] so now we have it inside the index page [27:50] instead and we could also go down now [27:52] and start accessing all these methods [27:54] and properties as we did before so I can [27:56] say we have card number one and I want [27:58] to point to a property which is called [28:01] get was it called card type I think it [28:03] was card type vehicle type okay so we're [28:06] going to say vehicle type and then we [28:07] can actually Echo this one out if you [28:09] wanted to [28:10] and if I were to go back inside my [28:12] website and actually go back to the [28:14] front page so the index page you can now [28:16] see that we get car so I hope things are [28:18] starting to make sense now that we have [28:20] a class that has a bunch of information [28:23] about this class and a bunch of methods [28:27] so again functions so to speak but now [28:30] the way we've been doing things as it is [28:32] right now is not really how you should [28:34] be doing things because right now we're [28:37] accessing this property directly simply [28:40] by just referencing to vehicle type and [28:42] then printing it out inside the website [28:44] and this is something that I see a lot [28:46] of people they just do like they'll go [28:48] in and if they want to access vehicle [28:49] type they'll just make it public and [28:51] then they'll access it this sort of way [28:52] and that is not actually how the proper [28:57] um it's not best practice to access it [28:59] in this sort of way what we can do is [29:01] instead of accessing these properties [29:03] directly by referring to them like this [29:06] just making them public and then [29:07] printing them out we can create [29:09] something called a get and a Setter [29:11] method so we go down here and we can [29:13] actually create a comment so we can say [29:15] getter and Setter methods and what we're [29:20] basically going to do here is we're [29:21] going to create a method that is going [29:23] to access a property and we're going to [29:25] create a method that is going to change [29:27] the property so if I want to change the [29:30] brand or the color to something else we [29:32] can call upon one of these methods here [29:33] and actually do that so what I'm going [29:35] to do is I'm going to create a function [29:37] and this one is going to be public so [29:39] we're going to say public [29:41] function I'm going to call this one get [29:43] and then the name of the property so get [29:46] brand with a capitalized B and in this [29:49] order way we can now create a getter [29:51] method and just simply go in and say we [29:53] want to return variable this 0.2 our [29:57] brand and this would be a basic getter [30:00] method which now means that I can [30:02] actually go inside my index page and [30:04] instead of accessing one of these [30:06] properties directly I can actually go [30:07] and say I want to do a get brand [30:11] and access it in this sort of way so we [30:13] were to go back inside the website you [30:14] can now see if we get the brand which is [30:16] called BMW and this is called a getter [30:19] method but we do also have something [30:20] called a Setter method so I'm going to [30:22] copy paste this one down below and I'm [30:24] going to call this one set brand so [30:27] instead of git brand we set it so we we [30:29] actually set it to a new value so to [30:32] speak so what I can do here is I can go [30:34] inside and instead I'm going to say I [30:36] want to refer to this [30:39] Brands and I want to set it equal to a [30:42] new value which is going to be variable [30:44] brand and this is actually going to be a [30:48] parameter that we can pass inside this [30:49] method when we call upon it outside the [30:52] class or after we instantiated the [30:54] object so to speak so if we were to go [30:56] back inside the index page what I could [30:57] do is right before [30:59] is instead of calling upon get brand I'm [31:02] going to call upon set brand and I'm [31:04] going to change the value to for example [31:07] a Volvo [31:08] so we were to do this again what we're [31:11] doing is we're creating an object then [31:13] we're going to change the brand of the [31:15] card to Volvo so now it's no longer BMW [31:17] and then we're printing it out inside [31:20] our website so it will go back down [31:21] refresh you can now see that it is [31:24] called Volvo and this is how we do [31:26] things in the proper best practice sort [31:29] of way okay so instead of changing [31:31] things directly inside the properties up [31:33] here which in other languages by the way [31:35] is called a field and these down here [31:38] are actually called properties in other [31:40] languages but because PHP is doing [31:42] things a little bit different and we're [31:44] basically just creating a normal method [31:46] and just calling them get brand and set [31:47] brand and the syntax isn't really much [31:50] different than just creating a method [31:51] you know any other method like this one [31:53] down here you can see we do the same [31:55] thing we just create a public function [31:56] get car info and then up here we just [31:58] create a public function get brand so [32:01] it's the same process in PHP they [32:03] decided to just call these together and [32:05] set of methods because technically these [32:07] are methods but in other programmer [32:09] languages if you do have a background in [32:11] for example C sharp we do not have a [32:13] specific syntax you know where we go in [32:15] and actually say we call upon a get [32:17] method and then we create a get method [32:19] so a little bit of confusion for new [32:21] people here but if you are a person with [32:23] a little bit of experience [32:25] it's a little bit different so now a [32:27] couple of things to talk about when it [32:28] comes together and set of methods [32:30] because yes you do need to create one [32:32] for all the different properties so now [32:33] we created one for the brand which means [32:35] we need to go down and also create one [32:37] for the colors so instead of get brand [32:39] we're going to say get color I'm going [32:42] to do the same thing down for the set [32:43] method so we're going to say set color [32:45] I'm going to change it to color instead [32:48] of brand so we're going to say color so [32:50] we'll point to the color here change all [32:52] the brand names to color and just like [32:54] that we now have a getter and a Setter [32:56] method for our color and just for now [32:59] let's go and go back up inside the [33:00] properties and just go and delete this [33:02] last property here because this was more [33:03] just to demonstrate that we could just [33:05] set it equal to a certain value to begin [33:07] with if we wanted to but we don't really [33:10] need it for the rest of these examples [33:11] in this video here so I'm just going to [33:13] delete it here so we don't need to [33:14] create a geta and a set of method for [33:15] that one either so a couple of things [33:18] about a getter and a set of method [33:19] because you may have some questions here [33:21] one thing is performance because yes [33:24] this is kind of like a little bit more [33:27] complicated instead of just saying oh [33:29] I'm just going to set this one to public [33:30] and then I'm going to access this [33:31] directly inside my code by just saying I [33:33] want to reference to color you know that [33:36] would just be the easy route right doing [33:39] it this way using a getter and a setup [33:40] method is a slightly more complex way to [33:43] do things because we are creating more [33:45] code essentially another thing is [33:48] performance which is slightly more [33:51] performance heavy not a lot just a [33:53] little bit but the benefits of using [33:55] getter instead of methods are much [33:57] greater than not using them one of the [34:00] things you can do using getter and setup [34:02] methods and again you can just sort of [34:03] like Google what are the benefits if you [34:05] want to see more but one of the benefits [34:07] is I could for example go inside my set [34:10] color and say I'm only going to allow [34:12] certain colors to be set inside when we [34:16] change the color so if I were to go down [34:18] inside my set color what I could do is I [34:20] go inside and say I want to create an [34:21] array so allowed colors [34:24] and then I can set it equal to an array [34:27] semicolon send it down and I do have [34:30] some values that I can just paste in [34:32] here so red blue green and yellow and [34:35] basically what we're doing now is we're [34:36] running a if condition [34:38] so we're going to say if a certain [34:40] condition is true so if we have an array [34:44] in underscore array which is a built-in [34:47] method or a built-in function inside PHP [34:49] and then I can simply go in and say well [34:51] if this color that we paste in and feed [34:54] this method here does exist inside this [34:57] array up here then I do want to allow [35:00] for this color to be changed so in this [35:02] case here I can go in and paste this in [35:04] and now we're only changing the color if [35:07] we assign one of these colors up here so [35:10] if you use a setup method we can now go [35:12] in and actually run some custom code and [35:14] the same thing goes for getter methods [35:15] before we actually do return or change [35:19] the value inside our properties up there [35:21] so again if I were to go inside my index [35:23] page here and say I want to get access [35:25] to set colors I'm just going to go and [35:27] copy it here and I'm going to say [35:29] instead of get brand we're going to go [35:31] and access our set color you can [35:33] actually delete this one right here and [35:34] in this case I'm going to change it to [35:36] Green so I'm going to copy paste this [35:38] down and we don't actually need to Echo [35:40] out the first one here because this one [35:41] is just changing the value but this one [35:43] down here is going to be our get method [35:45] so we're going to get the color and [35:47] simply run that one so in this sort of [35:49] way we're changing the color and then [35:51] we're echoing out the color afterwards [35:52] using the getter method and let's [35:54] actually go and change this away from [35:55] Green because that is actually what it [35:57] is already so let's say yellow instead [36:00] so now we created an object that has a [36:02] green car inside of it and I say I want [36:04] to change it to Yellow so if it were to [36:06] do that and go inside the website you [36:09] can now see that we get yellow because [36:10] that is allowed inside my array but [36:13] let's say I want to change it to [36:15] something like white because I don't [36:16] actually have that inside my array so it [36:18] would go back inside the website you can [36:20] see it's now green because we're not [36:22] allowed to change it to White and this [36:24] is one of the big benefits of using [36:26] getter and setup methods and this is why [36:28] you should be using these instead of [36:30] just accessing these properties directly [36:32] every single time so again just to [36:34] summarize what we have here we have a [36:36] class that is called car and we have [36:38] Properties or Fields as they're also [36:41] called then we have a Constructor method [36:43] which is something that actually assigns [36:45] values to these properties when we [36:47] create the optic for the first time then [36:50] we have something called a getter and a [36:52] set of method for each property which is [36:54] going to go in and either get the [36:56] property or it's going to change the [36:58] property then we have something called a [37:01] method which basically is just a [37:02] function [37:04] again doing this that is directly [37:06] related to this class here and that is [37:08] really the basics when it comes to a [37:10] class okay there is much more you could [37:12] talk about but this is what you need to [37:13] get started with object oriented PHP [37:16] classes but we're not done yet because [37:19] now your next question might be well [37:20] okay Daniel what about a more realistic [37:23] example because this is talking about [37:26] you know producing a car but let's take [37:28] for example if I have a sign up system [37:30] inside my website because right now I do [37:32] have a signup system video that shows [37:34] how to create one using procedural PHP [37:36] but we're going to do a much more [37:38] simplified version in this little [37:40] example coming up next that is going to [37:42] show you how to create a very simplified [37:45] and unsecure signup system again just [37:49] because I want to cut this video a [37:50] little bit short I don't want to extend [37:51] this because it's not a sign up tutorial [37:53] but I do want to show you how to create [37:55] a signup system a very basic one using [37:58] object oriented PHP so you can see the [38:00] purpose of how we use classes instead of [38:03] procedure PHP so let's go ahead and [38:06] close down this car and what I want to [38:08] do instead is I want to go inside my [38:10] classes and first of all I'm going to [38:12] create a new file and call this one dbh [38:14] again with the capitalized d so d b h [38:18] dot PHP and this is going to be my [38:21] database Handler so I'm going to open up [38:23] my PHP tags go down create a class and [38:27] I'm going to call this one dbh now this [38:30] one is going to connect to my database [38:31] because you know when it comes to [38:33] getting information from a database or [38:36] changing information or inserting users [38:38] and that kind of thing we do need to [38:40] have a database connection and typically [38:42] the way we would do that is to have some [38:45] variables that would have for example [38:46] the host name the database name the [38:48] username and the password right for the [38:50] database and the way we just simply do [38:52] that inside a class is just to put them [38:54] in as properties we're going to say that [38:56] private host private database name [38:58] private username private password and [39:00] then we just have localhost the name of [39:02] the database and then the username [39:04] password okay so these are the [39:05] properties inside the class [39:07] what we then do is just basically create [39:09] a method that is going to go in and [39:12] actually connect to the database again [39:13] I'm just sort of copy pasting things in [39:15] here just because we don't want to write [39:17] this out this is not what the tutorial [39:18] is about but as you can see we just have [39:20] a very basic uh function or method in [39:24] this case here which is protected and [39:26] that will be explained in just a second [39:28] that is right now simply going in and [39:30] creating a PDO object which is a [39:32] connection to our database so we create [39:35] a new PDO object and we just fill in the [39:37] information so we have the mySQL [39:39] database we have the hostname pointing [39:41] to this property up here which is the [39:44] property up here so instead of just you [39:45] know having these being variables like [39:47] we did inside my procedural example in [39:50] that other video that I mentioned we're [39:52] just going to refer to these properties [39:54] up here then below here we just go in [39:56] and change some of the attributes to [39:57] make sure that we have some error mode [39:59] settings you know so we have you know [40:00] exceptions and such things if something [40:03] were to go wrong and then we do also [40:04] want to make sure we go down here and [40:06] actually return the this PDO connection [40:08] and that is one of the differences [40:10] between the last procedural video and [40:13] this one is we do actually to return the [40:15] connection [40:16] inside this method here and then again a [40:19] error message if something were to go [40:20] wrong so a very basic PDO connection [40:23] that we have talked about many times in [40:25] previous courses and just to mention [40:27] here because we have talked about [40:29] creating a PDO object that is going to [40:31] connect to our database in that [40:33] procedural course and I said don't worry [40:34] about it we don't need to talk about [40:36] Optics yet now you might be able to see [40:39] that okay so when we create this object [40:41] we just basically instantiate a new [40:44] object based off a PDO class that is [40:47] built into PHP with these parameters [40:50] here so this is the Constructor method [40:53] that is going to take information and [40:54] assign it to properties inside this PDO [40:59] connection object here so you might [41:00] start to see how this works you know [41:02] just by knowing how to create classes [41:04] and objects and that kind of thing so [41:06] let's go to talk about protected and the [41:09] name up here so basically now I just [41:11] called this method connect so instead of [41:13] referring to PDO in the future when we [41:15] have to connect to a database then we [41:18] would just use connect instead and I did [41:20] actually set this one to protected not [41:22] public and the reason for that is that [41:25] this is a very sensitive class okay this [41:28] has our database connection which means [41:31] that any class should not be able to [41:33] access this particular method again [41:36] we're trying to encapsulate our code as [41:38] much as possible to make sure that we [41:40] don't accidentally you know or maybe if [41:42] you're working with another developer [41:44] that they don't accidentally go in and [41:46] mess with something inside this class [41:48] here so we want to make sure we [41:50] encapsulate things and say that okay [41:51] only child classes of this class are [41:55] allowed to connect to our database so [41:58] what we can then do is save this file [42:00] and I can go in inside my classes folder [42:03] and create a new file I'm going to call [42:06] this one sign up [42:08] dot PHP again with a capitalized s [42:11] because this is a class file I'm going [42:14] to open up my PHP code [42:16] gonna go down and create a class I'm [42:18] going to call this one sign up so now [42:21] what I need to do here is talk a bit [42:22] about how we do things in procedural PHP [42:26] because that will make it make more [42:27] sense in just a second so typically [42:30] inside a index page you know we would [42:32] have a HTML form so let's just go and [42:34] delete everything that we have here and [42:36] say that we have a HTML form okay very [42:40] basic example we also need to include a [42:42] method so this is going to be a post [42:44] method so inside our project we would [42:47] actually have another folder so we're [42:49] going to say we have includes folder [42:51] that is going to have any sort of [42:52] include file so this is just going to be [42:54] a bunch of PHP files that just runs some [42:56] PHP code so again if we were to submit [42:58] this form we send the data to include [43:01] file which in this case here is going to [43:03] be called sign up dot Inc dot PHP and do [43:08] note here that I'm not capitalizing [43:10] anything because this is not a class [43:11] file this is just a basic PHP file okay [43:14] to open up the code here go down below [43:18] and we just basically do what we need to [43:20] do again it's very important to point [43:22] out here we're just doing basic PHP that [43:25] we would also do if this was procedural [43:26] PHP here okay so this is not object [43:28] oriented any sort of way uh so far okay [43:32] so in here what we would typically do is [43:34] run a if condition that basically goes [43:37] in and checks for a request method so [43:39] did we actually access this page [43:40] correctly by submitting a form which in [43:44] this case should be a post method which [43:46] in this case we do because this is a [43:48] post method so if we access this page [43:51] directly using a post method then I want [43:53] to go in and actually grab the data that [43:56] the User submitted so we do have again [43:58] I'm just going to copy paste my my node [43:59] over here so we do have a username and a [44:02] password so we just basically going to [44:04] say we have this post method called [44:05] username and a post method called [44:07] password and then we just basically want [44:10] to do something with this information we [44:12] could sign up the user by simply go down [44:13] and running some PHP code to sign up [44:16] user inside our website but let's go and [44:18] do that to object oriented way instead [44:20] so what I'm going to do here is I'm [44:23] going to require first of all my [44:25] database file because we need to connect [44:27] to the database so we need to have that [44:29] class available to us right so we're [44:31] going to say we want to go back One [44:32] Directory because we want to go back and [44:34] then go inside our classes folder and [44:37] inside our classes folder we'll go to [44:38] access our dbh DOT PHP file then we're [44:41] going to do the same thing but this time [44:43] we're going to access our signup file [44:45] and the order here does matter because [44:48] we do need to have the database class [44:49] first and then the signup class and the [44:52] reason for that is that the signup class [44:54] is going to be a child class of the [44:56] database class so the parent class goes [44:59] first and then the child class goes [45:01] after and again we haven't really talked [45:02] about how to make this sound of Class A [45:04] Child class we'll do that in just a [45:06] second okay but the order here does [45:09] matter so what we need to do now is go [45:11] back inside outside of class and we need [45:13] to create our properties so we need to [45:15] ask ourselves okay so in order to sign [45:17] up this user what properties do we need [45:19] to have so which variables do we need to [45:22] have and this is actually told to us [45:24] already because it would go back inside [45:26] my sign up the link to PHP file we have [45:29] two variables here so we need to use the [45:31] information that the user gave us in [45:33] order to sign up the user right so if we [45:35] were to go back inside the sign up class [45:36] I'm just going to create two properties [45:39] which are going to be these two pieces [45:41] of information so I'm going to create a [45:43] private username and a private password [45:45] and these are going to be my properties [45:47] then we're going to create a Constructor [45:49] because we need to assign some data to [45:51] these properties because right now [45:53] there's nothing assigned to them so if I [45:55] were to create a public Constructor [45:57] which is going to take in a username and [46:00] a password and assign it to these [46:01] properties down here we can do that in [46:04] this sort of way so now if I were to [46:06] actually go back inside my sign up the [46:08] link.php file go down below here and say [46:11] I want to create a new object which is [46:12] just going to be called sign up just [46:14] because we need to call it something and [46:16] this one is going to be set equal to a [46:18] new instantiated object so this one is [46:20] going to be a new sign up class that we [46:22] are instantiating into an object and [46:25] we're going to paste in two pieces of [46:27] information which in this case is going [46:28] to be a username and we do also need to [46:31] paste in a password so just going to [46:34] paste that inside our class or inside [46:35] our object here so if I were to go back [46:37] inside my signup class we now just need [46:39] to ask ourselves well what do we need [46:41] this class to do well we need to set up [46:43] the user right so we need to have a [46:45] query that actually goes inside our [46:47] database and queries the database and [46:49] this is what we need to talk about [46:51] making a child class because right now [46:53] this signup form needs information from [46:57] another class in order to properly work [46:59] because we need to sign up a user which [47:01] means we need a database connection and [47:03] we do have a database connection inside [47:05] our dbh class so because this one has a [47:09] protected method which can actually [47:10] connect us to the database I can go [47:13] inside my signup class and include [47:15] something called hold extends inside our [47:19] class name up here and we're going to [47:20] extend our dbh class so now we're [47:24] basically saying that the dbh class is [47:26] the parent class and the sign of class [47:28] is a child class of the dbh class and [47:31] this now means that I can actually go [47:34] inside and access any sort of protected [47:37] methods inside this class here because [47:39] now we are a child class so I can use [47:41] this method here called connect in order [47:43] to connect to our database it is also [47:45] important to note here that the reason [47:47] we can do this is of course because we [47:49] need to have the database class first [47:50] actually accessed inside this document [47:53] and then the sign up class below this is [47:56] the same thing as if you were to take [47:57] all the code inside the deviates file [47:59] and put that right inside this file here [48:02] you know right where this line of code [48:04] is since they would just paste the [48:05] entire class in here and then we take [48:07] the sign up class and paste it directly [48:08] below so in this sort of way you can see [48:10] how the ordering matters because we need [48:12] to load in the database class first and [48:14] then the sign up class right so going [48:16] back inside the signup class what we can [48:18] do is we can actually try and insert the [48:21] user inside the database I can go below [48:23] here and say I want to create a new [48:26] private function or private method and [48:29] I'm going to call this one insert user [48:32] and basically inside this one we first [48:34] of all need to query the database so we [48:36] need to create a query statement I'm [48:38] just going to wrap everything here so we [48:40] create a query statement that basically [48:42] just goes in and creates an insert [48:43] statement into a user's table inside our [48:46] database we're going to include a [48:48] username and a password the values are [48:50] going to be placeholders because we need [48:52] to do this using prepared statements of [48:54] course because that is going to prevent [48:56] SQL injection so that is important and [48:59] then I'm going to go below here and just [49:01] do the same thing as we would using [49:03] procedural PHP so go in create a [49:06] statement which is a prepared statement [49:08] and I'm going to connect to our database [49:10] by using this and then point to connect [49:14] so in our procedural course we would [49:16] actually call this one variable PDO [49:18] because that is our connection but in [49:20] this case since we want to access this [49:22] method in here which is called connect I [49:25] need to refer to this class and a [49:28] connect method and it is important to [49:31] note here that yes even though we say [49:33] this class we do also talk about the [49:36] class that we extend2 so you can write [49:39] this class and then the connect method [49:41] but I do also want to mention another [49:43] thing here which is something that could [49:46] potentially cause you to run into a [49:48] error so let's say I were to go above [49:51] this particular method here and we're to [49:53] create another let's just call this one [49:56] a private method and if I want to call [50:00] this one connects and just create a [50:02] method inside my signup class called [50:05] connect then we run into a small issue [50:08] because if we were to point to this [50:10] class and then connect it is going to [50:12] prioritize this particular connect [50:14] method inside this class here and not [50:17] the one inside the parent class so what [50:20] you can do if you have a method with the [50:22] same name inside two different classes [50:24] instead of using this keyword is you can [50:27] use something called parent colon colon [50:29] which is going to refer to the parent [50:31] class called connect so in this sort of [50:34] way we can do things in a better way [50:36] because we don't accidentally access a [50:38] method from inside this class when we [50:41] actually meant to access a method from [50:42] inside the parent class so we can use [50:45] this keyword here instead of writing [50:47] this class okay so just a small example [50:50] just to demonstrate something so now [50:52] we're pointing to a connect method from [50:54] within this class or inside the parent [50:56] class and we just simply prepare the [50:58] query so again basic PHP here the is a [51:01] basic prepared statement then we bind [51:04] the parameters so we go in and say that [51:06] this placehold up here in this [51:07] placeholder is going to have filled in [51:10] some information and that is going to be [51:11] from the properties from inside this [51:14] class here so the properties that we [51:15] assigned up here then we just go down [51:17] and we execute the code which means that [51:19] now the user is being signed up inside [51:21] the website but we do need to talk about [51:23] something else because it is a very good [51:26] idea to separate functionality inside a [51:29] class in the same way as it's a very [51:31] good idea to separate functionality [51:32] using different functions so right now [51:35] we have one method that just basically [51:37] goes in and queries the database and [51:38] that is good so we have a insert user [51:41] method that only queries the database [51:44] but what about everything else because [51:46] we need to do error handling and you [51:48] know different conditions and just basic [51:51] PHP code to make sure that the [51:53] information submitted by the user is [51:55] actually correct so what we're going to [51:56] do is we're going to create another [51:58] method so I'm just going to copy and [51:59] paste the name here paste it below and [52:02] I'm going to instead go in and call this [52:04] something else so in this case we could [52:06] say sign up user and then inside setup [52:09] user we basically just run all the [52:10] different error handles that we need so [52:12] we can say error handlers you know like [52:14] checking if there's any sort of empty [52:16] inputs or if this is a proper username [52:18] or proper password that kind of thing [52:20] and then we just basically sign up the [52:23] user if that is the case [52:25] so again error Handler write another [52:27] comment that says if no errors sign up [52:31] user right so we have basic code in here [52:35] so before signing up the user let's go [52:37] and create just a basic error Handler so [52:39] what I could do is I can go above here [52:40] and say I want to actually check if [52:43] there's any sort of empty input [52:44] submitted so what I could do is I'm just [52:47] going to copy paste from my notes Here I [52:49] do have a method that basically just [52:51] goes in and checks is the username [52:53] property actually containing any sort of [52:56] value and the same thing for the [52:58] password inside you know the properties [52:59] inside this class here and if so then [53:02] return false because there's no error [53:05] messages but if they do not have any [53:07] sort of info just one of them then [53:09] return this as true which means that we [53:11] do have an error message and then we go [53:13] inside our sign up user function or [53:16] signup user method and then we go in and [53:19] run a basic if condition where we go in [53:21] and just run a if condition that checks [53:24] if this method word that we just created [53:26] up here is returning as true meaning [53:29] that there is an error so if there is an [53:32] error message then we just basically go [53:33] in and run a header function to return [53:35] the user to the document root which is [53:38] our main directory of the website and [53:41] then send them to the index.php file so [53:43] basically just sending them to the index [53:45] page if there is some sort of error [53:46] message and then we kill the script that [53:48] is going on right here then below here [53:50] if there was no error so if we did not [53:53] encounter this error message then I do [53:55] just want to go in and sign up the user [53:57] so we can just basically go in and say [53:58] that we have this method called insert [54:00] user which is up here which is the query [54:03] statement and just basically sign up the [54:06] user so now we have a very basic class [54:08] that can handle going in and grabbing [54:11] some user information and then assigning [54:13] data to it right inside the properties [54:15] using a Constructor method and then we [54:17] have a basic method that just goes in [54:19] and queries the database and actually [54:21] signs up the user and just keep in mind [54:23] this is a very sensitive method it [54:25] because we're interacting with the [54:27] database so this one is set to private I [54:29] don't want this to be run outside the [54:31] actual class okay that is why this one [54:34] is private and we do also have a private [54:37] method down here that is called is empty [54:39] submit because we're basically just [54:41] checking for a particular error Handler [54:43] so in this case here we don't need to [54:45] run this outside the the class right now [54:47] or outside the the object so it's okay [54:50] this one is private for now if you're [54:51] the later Point might want to run this [54:53] outside the actual class then just make [54:55] this one public but for now just private [54:58] is okay [54:59] again always make it private first and [55:02] then if you have a reason to use it [55:03] outside when you have created the object [55:05] then make it public for now we don't [55:07] have a reason to and then again we just [55:09] have a basic method down here that is [55:11] called signup user that is going to sign [55:13] up the user inside the website now one [55:16] thing we need to change here is that I [55:18] do need to run this method outside when [55:20] we have created an object because inside [55:22] my setup file I do need to go below here [55:25] and actually run a method from inside [55:27] this optic we just created so I'm going [55:29] to say we have an object called sign up [55:32] and I'm going to point to a method [55:34] called sign up user [55:37] parentheses but you can see we get an [55:39] error message huh undefined method and [55:43] that is because right now this one is [55:44] set to private so we do need to use this [55:47] method outside the object in order to [55:50] actually sign up the user so this one [55:52] has to be public so it would do that go [55:56] back inside you can now see the error [55:58] message disappears and now we can [56:00] actually run this method in order to [56:01] sign up the user and just like that we [56:04] have a basic signup system you know a [56:07] very basic one because we did Miss quite [56:09] a few things you know there was supposed [56:10] to be many more error handlers session [56:12] security all that kind of thing we [56:15] didn't even close off the statement and [56:18] the database connection even though you [56:19] don't technically have to but you know [56:21] it's a good practice to do so again very [56:24] bare bone example but just to prove a [56:26] point that you can use a class instead [56:29] of using variables and functions inside [56:32] your code in the procedural way there's [56:34] also one more thing I want to point out [56:36] here which is that inside our car [56:37] example we did actually went ahead and [56:39] created all these together and Setter [56:41] methods you should also do that of [56:43] course inside this one here you should [56:46] also have a getter and a Setter for the [56:48] username and password so with that we [56:50] now know how to instantiate an object [56:53] based off a class and actually have an [56:55] example of how to use this in a real way [56:59] instead of just showing you how to [57:01] create classes and objects and then [57:03] you're left for yourself to figure out [57:05] how to use them so in this case here I [57:07] wanted to give you an example of how we [57:09] could actually use them in the same way [57:11] as we would using procedural code okay [57:14] so with this little [57:17] crash course I guess in optic or into [57:20] PHP I hope you enjoyed this lesson and [57:22] had a better understanding of how this [57:24] works again this is like the [57:26] introduction to optic oriented [57:28] programming we haven't talked about [57:29] design patterns like the MVC model but [57:32] let's not get into design patterns now [57:34] just know there's something called [57:35] design patterns and a very popular one [57:37] is something called amec pattern which [57:39] is the one that I prefer to to use in my [57:41] tutorials here so hope this made sense [57:43] and I hope this gave you a not just an [57:46] idea about how to create object oriented [57:48] PHP but also how to use it inside your [57:51] code so with that said I hope you [57:53] enjoyed and I'll see you guys next time [57:56] [Music] [58:01] foreign [58:06] [Music]