The Autoloader

While there are many changes to PHP 5 that I love, if I were forced to choose a favorite, it would probably be this next thing I’m going to show you. It’s called autoload, and what it lets you do is define a way of including files dynamically based on the names of classes that your scripts need to use.

The basic idea is you devise your own class-naming convention such that based on the name of any given class in your project, you can say which file holds it. And then, you create a function named __autoload that takes the name of any class and builds an include statement that ought to give you access to it.

This lets you be sure that only the files that need to be included actually are, rather than having to include every single file that might possibly hold a class that some part of your script will need later. And best of all, you create the function once, and then so long as you keep your naming convention, you never need to think about including class files again!

Probably the most common classname convention is where you represent directory separators as underscores in your class names. So, for instance, if your class is named ‘Really_Big_Long_Class_Name’ then it probably resides in really/big/long/class/name.php. Personally, I tend to use a variant of this convention, where instead of ending my class files with .php, I end them in .class.php. So anyway, here’s what the __autoload function that does that might look like:

{show example code}

However, there is a drawback to this technique. Namely, if you define a function __autoload, you can only have one autoloader function. That might not sound like much of a drawback, but think about what happens if you want to make or use a 3rd-party plugin. Well, a plugin needs to define how its own classes are going to be included, and redefining its own __autoload function for the entire website isn’t going to fly. Technically, it’d cause a fatal error if the __autoload function has already been declared somewhere else. Like any other function, you only get to define it once.

So for more complicated websites, you either go back to including all your class files manually, or you switch to the very cool spl_autoload_register function.

spl_autoload_register lets you define a stack of functions that each get checked, one at a time, in order, until the class sought is found. You can call it as many times as you like, adding a different path conversion algorithm each time. This is the strategy I recommend you use in your own code.

Now, I haven’t called your attention to it, but you may have noticed that in almost every script I’ve written that tests some class or another, I have a single include statement. All it says is include ‘autoloader.php’; . And my classes get imported correctly every single time.

Let me show you how autoloader.php actually works. Notice, I DON’T use the code example I just showed you. I could, and it would work, but this is something even better.

Remember how I said that the convention of representing directory separators as underscores in the class name is the most common one? Well, it’s SO common, that the PHP engineers provided it as a default. When I called spl_autoload_register with no arguments, it told PHP to go ahead and register its built-in autoloader. Because it’s actually built into the language, and coded in C, rather than PHP, this is actually going to perform file includes faster than the exact same algorithm coded into a PHP function.

There’s a couple other important lines in here. The first line, ‘set_include_path’ was necessary to make sure that my classes directory, where I’m keeping all my class files, is one of the include paths PHP checks when I try to use the include statement. The other line, spl_autoload_extensions, tells PHP that my class files will end in .class.php.

And that’s really it! If I want to use the exact same naming convention, but separate my class files into several different folders, I would just need to call the set_include_path again for each folder that might contain a class file.

January 24 2010 06:39 pm | Schmategories

Comments are closed.