Traditionally, in PHP, when we want to use a class file, we have to require or include at the head of the document:
<?php require_once('../includes/'); require_once('../includes/'); require_once('../includes/'); ...
But once there are too many documents to be called, you have to write a line every time, which is not beautiful to look at. Is there any way to make the PHP document automatically load?
<?php function __autoload($class_name) { require "./{$class_name}.php"; }
Yes, you can use PHP's magic function __autoload(). The above example is to automatically load the PHP file in the current directory. Of course, in reality, we are more likely to use this:
<?php function __autoload($class_name) { $name = strtolower($class_name); $path = "../includes/{$name}.php"; if(file_exists($path)){ require_once($path); }else{ die("the file {$class_name} could not be found"); } }
That is, a certain file name case is processed, and then check whether the file exists before requiring. If it does not exist, it displays custom information.
Similar usages are often seen in the framework of private projects, or single projects. Why? Because you can only define a __autoload function, in multi-person development, different developers cannot use different custom autoloaders, unless everyone agrees in advance, they all use a __autoload, and if changes are involved, they will be synchronized, which is very troublesome.
It is also mainly because of this, there is good news that this __autoload function will be deprecated in version 7.2 PHP.
Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
Then instead is something called spl_autoload_register(), which has the advantage of customizing multiple autoloaders.
//Use anonymous functions to autoloadspl_autoload_register(function($class_name){ require_once('...'); });
//Use a global functionfunction Custom() { require_once('...'); } spl_autoload_register('Custom');
//Use a static method in a classclass MyCustomAutoloader { static public function myLoader($class_name) { require_once('...'); } } //Send array in, the first is the class name and the second is the method namespl_autoload_register(['MyCustomAutoloader','myLoader']);
//It can even be used on instantiated objectsclass MyCustomAutoloader { public function myLoader($class_name) { } } $object = new MyCustomAutoloader; spl_autoload_register([$object,'myLoader']);
It is worth mentioning that using autoload, whether it is __autoload() or spl_autoload_register(), compared to require or include, the advantage is that the autoload mechanism is lazy loading, that is, it does not call all the files for you as soon as you run it, but only after you use which file, such as which file you have new, will load the corresponding file through the autoload mechanism.
Of course, spl_autoload_register is often used in laravel, including various packages, for example:
/** * Prepend the load method to the auto-loader stack. * * @return void */ protected function prependToLoaderStack() { spl_autoload_register([$this, 'load'], true, true); }