9: Load Classes Automatically In OOP PHP | Object Oriented PHP Tutorial | PHP Tutorial | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so up until now we've been doing things in a very specific way when it comes to the path to our files and the way that we actually load in our classes inside our website and all of that is going to change today and I hope this is something that's not gonna mess with the hip too much but this is a better way to do things so we're just gonna go and do it in a slightly better way in this episode here so up until now what we've been doing is every single time you wanted to load in a class inside out documents here we do need to include the classes at the top of the page of every single document that we want to load in the classes in now this is an issue because in some cases if you build a very big website that has maybe let's say 20 different classes if you had 20 of these links under each other it's suddenly gonna start becoming slightly much so what we can do instead is that we do actually have a built in function inside PHP which allow for us to automatically load classes as soon as we instantiate a class inside our documents now in order to do this though I do want to recreate the path system that we have inside our root folder a little bit because when we start talking about something called namespaces the way we've been doing things up until now is something that is easier for you to get into if you came directly from my PHP series my procedural PHP series but when you talk about object-oriented PHP programming we don't really want to create a path system like we've been doing it till now so therefore I would like to do a new one for you now as you may know my general rule when it comes to including PHP files inside a website that isn't a direct page inside the website but more or less a include file that we just simply need to run to run a huge PHP script the way I do it is I create an includes folder inside my main directory and I put all those files in there and then I name them with the name of the file dot Inc dot PHP and the thought Inc - simply stands for includes I I think I explained all this in a previous episode but now the thing is that we technically had two types of PHP files now that are not shown inside the website we have include files which are just regular PHP files that run scripts and we also have class files so we might need to change the directories slightly in order to get a much more organized system and also a little bit later on we started talking about namespaces this system is going to be much better so what I'm going to do is I'm going to go inside my root folder and you can actually edit that directly inside atom in case you didn't know I can right-click on my folder and say new folder then I'm gonna go and create a folder called classes and now that we have this folder called classes I'm going to take all my class files and I'm gonna simply put them inside this class folder or classes folder but now I'll also gonna do something else because right now the files are still called house Inc the PHP and person Inc the PHP and I don't think I mentioned this but I do actually have two classes in this episode here person and house so what we're going to do here is we're also gonna go and change the names of these because it doesn't make sense to call them that Inc now that we're inside a classes folder so I'm just gonna go and right-click these rename them and just gonna go to name them as house dot class the PHP and I'm gonna rename person into person dot class dot PHP so now with this we have a completely new path system inside our project which is what I recommend you do in the future whenever we need to create a project using object-oriented PHP since it organizes the files much better so now let's actually start talking about the main point of this lesson here how can we load in our class files without having to call on them or include them every single time we need to load all the different classes inside our website here and the way we can do that is using a very simple built-in function that allow for us to like I said simply look for a new instantiated class and then it goes out a class just got instantiated maybe I should pull that class from a directory that we gave it so the way we're going to do that is I'm just gonna go and replace this code up here at the top now later on we will in this coat that I'm about to write inside a separate file that we can just send a link to that will load all the different classes but for now let's just go ahead and type it at the top here just what to make sense now the function we're going to be using is one called SPL underscore outer load onto score register which is a class like I said that goes and checks with these sort of things now there has been a previous function in the past called underscore outer load which is something that PHP is trying to not continue so if you've been using that one or somebody in the comments like to talk about something called an underscore outer load just know that it's going to be depreciated at some point in the future so inside the code team just gonna go ahead and say SPL underscore outer load are the score register parentheses and what we're going to do here is whenever we want to run a function we're just gonna go and pass that function inside the parentheses so at least the name of the function now we could if you wanted to create a function directly inside these parentheses but just for the sake of keeping things tidy and neat that's actually going to create the function underneath here so what I'm going to do is I'm gonna go and create a new function which is a user-defined function one that we create so I'm just gonna say we have function I'm gonna call this one my time axis spell out correctly how to loader parentheses and now inside these parentheses here we're going to pass in a variable and this variable is automatically going to get assigned as soon as we call on a new instance of a class so this variable here is a placeholder for whenever we call and try to create an object from a class so we're just gonna go and call so on something like class name and then what we're going to do is we're going to create the path to where we have all our class files saved because in order to actually load the class files we need to make sure we have the proper path to the place that this function here is going to be searching for classes that might get instantiated at some point inside our code so I'm going to create our first variable here I'm just going to call this one path and I'm going to go and set this equal to a string and it's going to be set equal to a path called classes because we just created a new folder called classes which is where we're going to put class files so you're gonna say /and then we're gonna go and create a new variable this one is just gonna be called extension because you also need to get the extension of the file so I'm just gonna go and say egg extension you could also just say ext for short if you want to shorten a little bit and I'm just gonna go and set this equal to the extension so right now we have dot classes that class not classes got that wrong dot PHP and then at the end here we're just gonna go and write the full path name so we could say variable full path I'm just gonna go and set this one equal to first of all the path because that's what we usually start the path with then we're gonna go to reference to the class name you have up here because remember we're passing in the name of the class we're trying to instantiate so we want to make sure we grab that name and also add it to our extension here so now we have three different variables to really using or to create the full path to the class that we're going to instantiate at some point in the future so what we're going to do now is we're just going to go to skip an error handler here really quickly just to show you what exactly it does and we're just gonna cordon include underscore once the class file that we're going to be linking to here so I'm just gonna go ahead and paste in the full path and there we go now the last thing needs to do before we can actually get this working is to actually reference to this function here up inside our SPL autoload register function so what we're going to do is we're going to go inside the parentheses write single quotes I'm just simply gonna paste in the name of this function here we just need to add in the function name as a string in order for to know which function we're referencing to here so after saving and just to show what exactly I'm echoing out inside the browser right now you can see that if I were to go down to the bottom here you can see we have two different objects we have one called person and one called house and I'm just simply spitting up the information of the person I created and the house I created so you know inside the browser this is how it looks like before we refresh the browser so if I were to refresh with the new path creation function we just created you can see we get the exact same thing and that's because it's automatically grabbing I'm just going to find it here it's automatically grabbing the file that we need to use when we instantiate the class called person and the same thing of course when it comes to the house down here so what we're going to do now just to make this a little bit easier I'm going to just copy this code delete it I'm gonna go inside a new file actually we can do that loop it easier and just go ahead and create it over here so we're going to go inside our includes folder create a new file and I'm going to call this one let's say how to loader Inc dot PHP and then I'm just gonna go and paste it in here of course we need to also open up our PHP tags that is one important thing we need to do and there we go so now that we have this we just simply need to link to the file in order for this to be grabbed so I'm just gonna say include let's go ahead and say include this file here called out to loader Inc dot PHP and of course the jewels need to make sure the path is correct so it's includes forward slash are loaded I think the PHP so now we're graphing this file here and refreshing the browser you'll see that we get the same thing nothing is changed it's just in a separate file so what we just need to do every single time need to outer load any sort of class is to include this one line inside all the difference files who might need to link to classes from and now some of you might be asking me well couldn't we just have taken all the different includes for all the classes we have inside the folder just make a list inside a separate file and just link to that one file just like we did here so essentially just put all the classes links in here well the difference is that every single time you create a new class you have to go inside this file and create a new line for that class whereas in this way you don't need to touch this file anymore so it is just much easier to do it this way so don't have to worry about linking to the classes at any point in the future or updating this document every single time you add new classes so doing it this way is just the better way now I did also mention era hand inside this function here so let's go ahead and talk about that really quick if I were to go ahead and make an error inside my coat let's say I want to look for a person called purse it's done because maybe I wrote something wrong inside the class name if I were to save this go inside my browser so you've noticed that we get all of these error messages here what we can do to shorten that a little bit just to give us a much more clearer image of what exactly is going on here is we can go inside our outer load of function and then I'm just gonna go ahead and go down right before we actually include the file and I'm going to run an if statement then inside the parentheses we're going to ask whether or not we cannot find this file we're looking for so I'm going to run a function called file underscore exists parentheses and because we're looking to see if we cannot find it I'm just gonna write exclamation mark in front of it here and what we're going to do is we're gonna go out make sure we include the full path inside file exists and then we're gonna go and say if we cannot find it then simply return false so what this is going to do is if I were to go inside the browser refresh the website you can see we get a much cleaner looking error message inside the browser here so this is an error message that I would just recommend you include inside this function here but it's really that simple so we can just go ahead and make sure we changed it back again so it doesn't give us an error now I did also at the beginning here mentioned something about namespaces and if you do know about namespaces I will just quickly show how to link to namespaces if you don't know about namespaces just don't worry about it for now it is something we'll talk about in the future but for now you don't need to know about namespaces because it is essentially just a way for us to categorize classes under a namespace to organize it better which is something that is more or less more required when it comes to larger projects so for now when we still talk about these small examples here we don't need to talk about namespaces but it is something we'll get into at some point when we talk about namespaces and using this outer load of function what we need to do is we need to make sure first of all that we have a namespace created inside our classes folder so what I would typically do is I would say that all the primary files that does need to be categorized under namespace to just go inside the global space here inside classes and any sort of files that actually do need to go under namespace let's say create a new folder called person we could call this a person's or something but just person for now and every single class that has something to do with people would then go under this namespace so let's say we have a class called CV are done in the class dot PHP if we had something called ancestry dot you would put all those classes under this namespace here so what I'm going to do is I'm going to take my class file called person and put it under this namespace so having done this I can go inside my person file and I can create a namespace so I'm gonna say name space call this one person closed it off here and now what is going to happen is if I were to go inside my website we're going to get an error message because it can't find the path that we link to inside our inside our outer loader function so we do need to write this slightly different to get it working so inside our index file whenever we want to call in a class and instantiate it what we need to do is we need to link to the namespace as well meaning that when we go in here I'm going to say we have a person namespace backslash and then I'm just gonna go ahead and link to the rest of the path that or to the class name that we're trying to get here so having done this if I were to go inside my browser refresh you can now see that we're getting it again so when you handle namespaces you need to do a slightly different here besides this this is what I want to serve when it comes to outer loading in your class files this is something we'll be doing in the future so I'm very much looking forward to see how many people dive into one of the future episodes not having seen these previous ones and then asking in the comment field what exactly am i doing with that function there why am I not loading in my classes so that is going to be exciting but this is how we do that so hope you enjoyed this episode and I'll see you in the next one [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 89,488
Rating: 4.9579549 out of 5
Keywords: php tutorial, mmtuts, php, classes, php classes, php auto load classes, spl_autoload_register, php spl_autoload_register, load classes automatically in php, use classes in php, link to classes in php, object oriented programming, object oriented php, object oriented php tutorial, object oriented php tutorial for beginners, php autoloader, autoloader, oop php autoloader, php autoload, autoload
Id: z3pZdmJ64jo
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Mon Jun 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.