Why spl_autoload_register is preferred for auto loading classes?

The biggest advantage of spl_autoload_register is that it allows multiple autoloader functions at a time. Ultimately it creates a queue of autoloader functions and then it runs through each of them in the order they are defined.

In the project if a framework code or other third party libraries have their own autoloader functions and user tries to use spl_autoload_register on top it this. In this case it will not conflict as spl_autoload_register allows multiple autoload functions.

So no worry about conflicts when using spl_autoload_register.

Basic spl_autoload_register code snippet

function __autoload_class($class){
    include_once $class.'.php';
}

spl_autoload_register('__autoload_class');

Comments