Autoload functions are called in sequential order, the first registered function is called first, the second one is called after and so on, until one of them finds the class
(unless $prepend
is used). If none do, a fatal error is triggered.
However, in your example, you are using require
instead of include
, so the first function will always fail with a fatal error if the file doesn't exist, so the second one will never be called. Even if you replace it with include
, if both files exist, only the first registered function will be invoked.
It's a good idea to use file_exists
or include
when using multiple autoload functions.
You can also throw exceptions in your autoload functions, so that you can handle undefined classes gracefully instead of killing the script if a class is not found. Per example:
function my_autoload($class) {
$filename = "class.{$class}.php";
if (!file_exists($filename))
throw new Exception("$class not found");
include_once $filename;
}
spl_autoload_register('my_autoload');
try {
$a = new Foo;
} catch (Exception $e) {
// Foo wasn't found
}
// continuing the script...
Obviously it depends on your needs, as throwing an exception will halt the execution of subsequent autoload functions.
Finally, it discouraged to use __autoload
at all, spl_autoload_register
providing a lot more flexibility that the former.