How to automatically load PHP classes using spl_autoload_register.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and welcome to this tutorial which is on how to load classes using spl autoload register now just to let you know this is part of a series called php from intermediate to advanced however i do want to make these videos self-contained so if you want to do the whole thing that's nice but if you just want to reference bits and pieces that's nice as well okay with that in mind let's have a fresh start shall we and i really think that's probably a good idea so i'm going to clear the decks right and i have um a folder called php training it's over in local host here it is right here okay and what we're going to do i know it's perhaps a little bit tedious but i would like to take a moment to just create a few classes together so we'll have a new file i'll call it first dot php with an uppercase f okay and then i want you to head into php and we're gonna say class first and then we're just gonna have a little function here called run first and we'll just say echo hello from first with a new line okay nice and easy so that is going to be first.php why can i not see this you know something i don't know why that happens once in a blue moon all right well whatever happened it's one of life's great mysteries we are here we are clear and we have a class all right let's make another one up now we're going to call it second now i did this in the last tutorial and it looks like i'm repeating myself but like i say i want the videos to be self-contained if possible so for the second one we're just going to do that and we're just going to have a little function here called run second and we're just going to say echo hello from second thank you very much indeed let's have another one and we'll call this one thud.php now again please make sure that the first letter of your file name is uppercase and in a few minutes i'll explain why so this is going to be class thud and we're going to have a little method here called run thud and i'm gonna say echo hello from thud all right so there we go we have three classes all ready to rock and roll now let's make up a file shall we and we'll call it index but we can have index.php in lowercase we can indeed so we're going to have a thing called index.php obviously if we refresh the home page it's going to give us our index thing and that's kind of nice now let's talk about loading of classes up until now what we've used is require once which is not a bad thing to use so you could say for example require once first and of course we could do the same for second and third i hope i'm not going too fast for you folks anyway we can do something like that and let's assume that we wanted to use the second class well we could just say something like a second is a new instance of second like that and then we could just say okay well second let's call upon run second if we save if we refresh there we go now that works that's good and well done if you can handle that however if you're building a large app such as an online shop you do not want this okay which is perfectly possible we really want to avoid that and find a more intelligent way to load stuff in so it turns out that there is something we can use and it's called spl auto load register now do you like comedy good because i'm about to give you a laugh if you search for this phrase here we go i'm going to read this right it says a register a function with the spl provided underscore underscore auto load queue if the queue is not yet activated it will be activated if your code has an existing underscore underscore auto load function then this function must be explicitly registered on the auto load queue this is because spl auto load register will effectively replace any character okay it's horrible right it's just ridiculous does anybody enjoy that does anyone even understand that well fear not because the web developer extraordinaire is here he is clear and i'm going to make you an expert on this subject to make things clear let's have a dc definition dc definitions are where we cut through the garbage and i talk to you like you're a human being and hopefully we can all walk away knowing something so here's your dc definition of spl auto load register what it does is it invokes um do you like the warden calls executes whatever makes you feel good right so what it does is it calls a function when a class is loaded in other words in other words it says to the computer right when you load a class right or when you try to load a class it says hey computer it says uh could you please do this and that's it that's what it does so let me give you an example okay and we'll clear the decks here nice fresh start and what we'll do is we'll just say spl auto load spl auto load register okay now the vibe is i was saying that when you load a class this thing is going to run and we are going to tell this thing what we want to happen when we attempt to load up a class so i'm going to chuck a little function in here and it's going to accept a thing called class and we're going to do that now that looks a wee bit weird but just relax it's really just an ordinary function right now this class thing is going to be automatically passed into this and the class will be the name of the class for example uh second that's the name of one of our classes right so when somebody tries to call a class so let's say for example we say okay how about if we say second is new second right so what's happened we're calling a class now because we have spl auto load register the computer is going to say hang on a minute they're calling a class i better do some stuff and it's going to run this now what would you like it to do well how about if we say something like require once and we can just load up second.php that would be good right that would be nice if that happened automatically well we can do that but of course we don't want it to just load second let's be smart let's put class in here and then we're going to add on a little dot php do you think life can be that simple do you think that we are living in a universe where things are really that easy let's find out we're gonna call upon run second we're gonna save we're gonna refresh and hallelujah hack the herald angels sing you know i don't want to be controversial or anything like that but that was a few lines of code right and it was kind of easy and it's a real source of frustration to me that the php community makes stuff like this really complicated you know loading classes and you go here then everywhere and people are using vendor auto loads and all sorts of you know composer dot files and everything and they just make life really really complicated the big news flashes you don't need to make life complicated and i hope that you'll continue with these videos because i'm going to show you step by step how php is really really easy and it's at its best when it's easy it's other people who make it complicated and you don't need to do that one other thing you'll also notice that class names start with an uppercase first character for example capital s and then you know the rest of the word second right well when we require in those files that means of course we're requiring a file that has an uppercase first character so now now you know why frameworks like tron gate and codeigniter require the files to have an uppercase first character i just thought i'd mention that hope you enjoyed this and i'll see you in the next one
Info
Channel: David Connelly
Views: 7,465
Rating: undefined out of 5
Keywords:
Id: 20nFAHJT2Qg
Channel Id: undefined
Length: 9min 29sec (569 seconds)
Published: Sat Oct 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.