The biggest drawback of __autoload is the inability to have multiple autoload methods.
Well, consider the following scenario, your project refers to someone else's project, your project has a __autoload, other people's project also has a __autoload, so two __autoload conflict. The solution is to modify the __autoload to become one, which is undoubtedly very cumbersome.
So we urgently need to use a autoload call stack so that SPL's AutoLoad series functions 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. Learn some of the functions of SPL first:
Spl_autoload is the default implementation of _autoload (), which will go to include_path for $class_name (. php/.inc)
In the actual project, the usual method is as follows (it is called only when the class cannot be found):
Example to Auto-load class files from multiple directories using the Spl_autoload_register method. It auto-loads any file it finds starting with class.
. PHP (lowercase), eg:class.from.php, class.db.php spl_autoload_register (function ($class _name) {//Define An array of directories in the order of their priority to iterate through. $dirs = Array (' project/',//project specific classes (+core Overrides) ' classes/',//Core classes Example ' tests/',//Unit test classes, if using php-unit); Looping through each directory to load all the class files. It'll only require a file once. If it finds the same class in a directory later on, it'll IGNORE it! Because of that require once! foreach ($dirs as $dir) {if (file_exists ($dir. ' Class. '). Strtolower ($class _name). php ') {require_once ($dir. ' Class. '). Strtolower ($class _name). php '); Return
} } });
The above describes the PHP __autoload and spl_autoload, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.