I try
function some_class($class) {
require PATH.'test'.$class.'.php';
}
function test_class($class) {
require PATH.'some'.$class.'.php';
}
spl_autoload_register("test_class");
spl_autoload_register("some_class");
$tst = new SomeClass();
SomeClass class locate in “PATH/some/SomeClass.php”, but spl_autoload_register call only “test_class” function and not call “some_class”. But if I change functions position, then it find new SomeClass.
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
$prependis used). If none do, a fatal error is triggered.However, in your example, you are using
requireinstead ofinclude, 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 withinclude, if both files exist, only the first registered function will be invoked.It’s a good idea to use
file_existsorincludewhen 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:
Obviously it depends on your needs, as throwing an exception will halt the execution of subsequent autoload functions.
Finally, it discouraged to use
__autoloadat all,spl_autoload_registerproviding a lot more flexibility that the former.