Detailed description of automatic loading of PHP files, detailed description of autoloading

Source: Internet
Author: User
Tags autoload

Detailed description of automatic loading of PHP files, detailed description of autoloading

Traditionally, in PHP, when we need to use a class file, we have to perform the following in the Document Header: require or include:

<?phprequire_once('../includes/functions.php');require_once('../includes/database.php');require_once('../includes/user.php');...

However, once there are too many documents to be called, you have to write a line each time, which is not beautiful. Is there any way to make PHP documents automatically loaded?

<?phpfunction __autoload($class_name){  require "./{$class_name}.php";}

Yes, you can use the PHP magic function _ autoload (). The above example is to automatically load the PHP file in the current directory. Of course, in practice, we are more likely to use this:

<?phpfunction __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, the file name is case-sensitive, and the user-defined information is displayed if the file exists before require.

Similar usage is often seen in private projects, or in the framework of a single project. Why? Because you can only define one _ autoload function, different developers cannot use different custom autoloader during multi-person development, unless you have mentioned it in advance, use a _ autoload command to synchronize versions when changes are involved. This is very troublesome.

The main reason is that the _ autoload function is about to be deprecated in PHP 7.2.

Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
Instead, it is called spl_autoload_register (). The advantage is that multiple Autoloaders can be customized.

// Use an anonymous function to autoloadspl_autoload_register (function ($ class_name) {require_once ('...');});
// Use a global function Custom () {require_once ('...');} spl_autoload_register ('custom ');
// Use the static method class MyCustomAutoloader {static public function myLoader ($ class_name) {require_once ('... ') ;}} // input the array. The first is the class name, and the second is the method name spl_autoload_register (['mycustomateupload', 'myloader']);
// You can even use class 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 (), the advantage is that the autoload mechanism is lazy loading, that is to say, it is not that you call all those files as soon as you run them, but that only you use which file, such as the new file, will load the corresponding file through the autoload mechanism.

Of course, spl_autoload_register is also frequently 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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.