The spl_autoload method in php5 is equivalent to implementing your own __autoload
<?<span>php </span><span>function</span> __autoload(<span>$classname</span><span>){ </span><span>if</span>(<span>is_file</span>(<span>$classname</span>.'.php'<span>){ </span><span>include</span> <span>$classname</span>.'.php'<span>; } </span><span>elseif</span>(<span>is_file</span>(<span>$classname</span>.'.inc'<span>){ </span><span>include</span> <span>$classname</span>.'.inc'<span>; } }</span>
It will automatically look for the .php/.inc file with the same name as $classname in the registration directory. Of course, you can also specify specific files by registering the extension
<?<span>php spl_autoload_extensions(</span>'.php,.inc,.some');
So how to automatically load spl_autoload? The method is
<?<span>php spl_autoload_register();</span>
spl_autoload_register has a $callback parameter. If not specified, it will automatically register spl_autoload. In order to search for more autoloading directories, you can set the autoloading directory in front of these codes
<?<span>php </span><span>set_include_path</span>(<span>get_include_path</span>() . PATH_SEPARATOR . 'some/path' . DIRECTORY_SEPARATOR);
These methods are commonly used in PHP frameworks.
__autoload is commonly used in automatic loading of class libraries
This is the method mentioned on the Internet. According to the class name, find the class file, and then require_one
spl_autoload_register()
The biggest flaw of __autoload is that it cannot Multiple autoload methods
Okay, think about the following scenario. Your project references a project of someone else. There is an __autoload in your project, and another person’s project also has an __autoload, so there are two __autoload conflicts. The solution is to modify __autoload to become one, which is undoubtedly very cumbersome.
Therefore, we urgently need to use an autoload call stack, so that the autoload series functions of spl appear. You can use spl_autoload_register to register multiple custom autoload functions
If your PHP version is greater than 5.1, you can use spl_autoload
This is a PHP function similar to autoloading, such as __autoload, but this can only pass in our NEW class name. If you want to call your own defined function during NEW, you can use
spl_autoload_register