Lessons: 18Length: 2.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.3 Autoloading Through SPL

For very small projects, manually including files and classes is manageable. But once you start to work on bigger projects, things can get very messy very quickly. Spl_register_autoload to the rescue!

3.3 Autoloading Through SPL

Hello, and welcome back to php oop fundamentals. During this course we've worked on a small number of files. Now with so little files, this is a piece of cake to manage. We can easily include them all inside of our own x.php file. So yes for very small projects, manually including files and classes is manageable. But once you start to work on bigger projects, things can get messy very quickly, and manually including an entire code library, for instance, is basically unmanageable. Luckily, as from PHP 5, we're blessed with auto-learning, and that sure makes learning classes a whole lot easier. An auto-loader is called automatically if your code contains a class reference that isn't declared yet. Now the auto loader searches and loads the appropriate file automatically. Let's see this in action, shall we? Well first at the top of this page, I'll remove all require statements except the one for helper.php because that will contain our auto-loader function. Let's have a look in the browser. Ouch, yeah. That code is broken, alright. Our code is looking for classes in files that have not been included. Okay, so let's get started on that auto-loader now. To do so, we need to open up our helper file. An auto-loader is basically a function that contains a certain set of rules and filters. So let's create one now. We'll just call it auto-load and that needs to be a function and it will accept a single parameter. Let's call that className. Okay, just clean that up. Now I can call this function whatever I want to, but autoload just seems appropriate, okay? First off, we'll need to construct a file name to search for. Now I've conveniently named our files after the classes they contain so all we have to do is add a file extension. Good. Let's create a file variable. And why don't we just include an absolute path. So we'll do File which will give us the current directory. Now that needs to be followed by a slash. Then the class name, of course, followed by another string and that will be .php. So now that we have our file name, we can do a simple check to see if it actually exists. So we'll do if file exists, and all we need to do is drop in the file name there. And then, if it does exist, let's just require that file. Okay, and I think that should take care of things. Now, how can we make sure this function is actually run when we instantiate an object that is not present yet? Well, that's where the actual magic comes in. Just register our function with spl_autoload_register like so, and all we need to do is pass in the function name, and Bob's your uncle. So just to recap, we construct the file name, and I see I forgot two underscores here, directory name followed by a slash followed by classend.php. So if we call a user class the file would be the path to the file and then user.php. Then we check to see if that file exists and if it does, we require it. Let's see this in action. And that all seems to work just fine despite the fact that we're now not requiring our user and validator classes at the top of index .php anymore. So yeah, auto loading can make your life much easier, come to think of it there are even more efficient ways of auto loading using name spaces. So first, let's see what name spaces are exactly, and we'll cover that in the next video, I'll see ya there.

Back to the top