Autoloading with PHP’s SPL library

I read Rafael Dohms’s post “SPL: a hidden gem” a few days ago and learned of a better alternative to PHP’s __autoload function using SPL instead and thought I’d share this here along with some more details about the spl_autoload_register() and spl_autoload_functions() functions.

The useful thing about using spl_autoload_register over __autoload is that it allows the use of multiple autoloaders; __autoload can only have the single autoloading function.

Using a class

Rafael’s example looks like this:

class MyLoader{
 public static function doAutoload($class){
 // autoload process
 }
 }
 
 spl_autoload_register( array('MyLoader', 'doAutoload') );

Because an array is passed to the spl_autoload_register() function it knows to call MyLoader::doAutoload($class) when autoloading.

Using a function

It a string is passed instead of an array then it’s a function which is being called: therefore you can either make the autoloader a function or a class. A function example:

function MyFunctionLoader{
 // autoload process
 }
 
 spl_autoload_register('MyFunctionLoader');

If the autoloading function doesn’t exist

When I was testing this out I made the mistake of writing some code passing the function two parameters instead of an array and got the following error:

Fatal error: Uncaught exception 'LogicException' with message 'Function 'MyLoader' not found' in /common/examples/spl-load.php:9
 Stack trace:
 #0 /common/examples/spl-load.php(9): spl_autoload_register('MyLoader', true)
 #1 {main}
 thrown in /common/examples/spl-load.php on line 9

This is because if you pass a string as the first function it looks for a function and not a class. If you want a class to be used make sure you pass array(‘class’, ‘function’) and not ‘class’, ‘function’

Add __autoload()

When using spl_autoload_register the existing __autoload() function is not automatically added to the queue. You need to add it yourself. For example:

if(function_exists("__autoload")) {
     spl_autoload_register("__autoload");
 }

Using multiple autoloaders

The following code will register all three of the above autoload functions:

if(function_exists("__autoload")) {
     spl_autoload_register("__autoload");
 }
 spl_autoload_register(array('MyLoader', 'doAutoload'));
 spl_autoload_register('MyFunctionLoader');

If a new class is created and doesn’t already exist in code, then the above functions will be called in the order they are registered and will stop as soon as the class is loaded. In the above example they’ll be called in this order:

  1. __autoload()
  2. MyLoader:doAutoLoad()
  3. MyFunctionLoader()

Showing which functions are registered

Use spl_autoload_functions() to get a list of functions that are registered.

$functions = spl_autoload_functions();
 print_r($functions);

With the examples above registered, this will be the output from the array:

Array
 (
 [0] => __autoload
 [1] => Array
 (
 [0] => MyLoader
 [1] => doAutoload
 )
 
 [2] => my_loader
 )