How does spl_autoload_register () work?

1

I have been reading but it is not clear to me how the files are loaded automatically. My question would be if you load them all or are loading as they are required in the application.

    
asked by prueba01 13.04.2016 в 21:36
source

1 answer

2

It is the function that is executed before showing an error of type Undefinded Class. That is, if within your App you call a class that you did not previously include it with either Include or Require, before showing a Fatal Error, the spl_autoload_register function is executed, which in my case is responsible for finding a file with the same name as the class and include it to avoid mistakes. It is the base of the Autoloaders.

spl_autoload_register(function ($class) {
        $appPath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'App';

if (file_exists($appPath . DIRECTORY_SEPARATOR . $class . '.php')) {
    include_once($appPath . DIRECTORY_SEPARATOR . $class . '.php');
}

});

    
answered by 15.04.2016 в 00:20