Thoughts about code and other things that interest me
Auto loading php classes

Edit: Don’t do it this way. See my new post

Tired of long lists of require/include statements to include class files? You can set up your code to automatically load classes when you call them. This feature is available in PHP5. There are a few different ways you can go about it though.

h4. Defining an __autoload function

Probably the easiest way to set up autoloading is to define your own __autoload function.

Whenever PHP encounters a class name that is not already defined, it will look for your defined __autoload function, call it, and pass the name of the class to it. But what if your classes exist it multiple directories? You could write a slightly longer __autoload function. Let’s say you had classes in two different directories: lib1/ and lib2/.

This is a bit better. But now you have to maintain a list of lib directories inside the __autoload function. PHP offers another way to autoload classes: spl_autoload_register. This allows you to define your own autoload function without naming it __autoload and you can also create as many of these functions as you need.

But this results in redundant code and the paths are still maintained inside the functions. If you want to set the autoload paths but encapsulate the dirty work, something else is needed. I created a class to address these problems. You can download it here.

Using AutoLoader

You still manage your autoload directories, but you do not have to fiddle inside functions to do so. And, you don’t have to maintain multiple autoload functions. There are a few downsides to this approach. In general, any autoloading is going to cost you some performance. Also, if your list of directories set with the registerDirectory method is long, I would imagine performance may suffer.

blog comments powered by Disqus