PHP File Auto-loading autoloading detailed

Source: Internet
Author: User
Tags autoload autoloader deprecated
Traditionally, in PHP, when we want to use a class file, we have to require or include in the document header: This article mainly introduces the automatic loading of PHP files (autoloading) related knowledge points and detailed usage, There is a need for this friend to refer to the next, I hope to be able to everyone.


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

But once the number of documents to be called, you have to write a line every time, look at it is not beautiful, what can be done to automatically load PHP documents?


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

Yes, you can use the Magic function of PHP __autoload (), the above example is to automatically load the current directory of PHP files. 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 is not found");}    }

That is, you do a certain file name case processing, and then check for the existence of files before require, and display the custom information if they do not exist.

Similar usage is often seen in private projects, or in the framework of a single project, why? Because you can only define a __autoload function, in multi-person development, do not have different developer use different custom autoloader, unless everyone agreed in advance, all use a __autoload, involving changes in the version synchronization , it's troublesome.

And the main reason for this is that the good news is that this __autoload function is going to be deprecated in version 7.2 of PHP right away.

Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on the feature is highly discouraged.
Then instead of a spl_autoload_register (), the advantage is that you can customize multiple autoloader.


Use the 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 in a class mycustomautoloader{  static public function Myloader ($class _name)  {    require _once (' ... ');}      } Pass an array in, the first is the class name, the second is the method name Spl_autoload_register ([' Mycustomautoloader ', ' Myloader ']);


It can even be used on an instantiated object class Mycustomautoloader{public  function Myloader ($class _name)  {  }} $object = new Mycustomautoloader;spl_autoload_register ([$object, ' Myloader ']);

It is worth mentioning that the use of autoload, whether __autoload (), or spl_autoload_register (), compared to require or include, the advantage is that the autoload mechanism is lazy loading, It is not that you call all of those files as soon as you run them, but only the ones you use, such as the new one, and then the autoload mechanism to load the file.

Of course, Laravel includes various package also often use spl_autoload_register, for example here:


/** * prepend The Load method to the Auto-loader stack. * * @return void */protected function Prependtoloaderstack () {  spl_autoload_register ([$this, ' Load '], true, true);}
Related Article

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.