PHP __autoload and Spl_autoload

Source: Internet
Author: User
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.

  • 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.