Learn Object Oriented PHP for Beginners | With Examples to Help You Understand! | OOP PHP Tutorial

Video Statistics and Information

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