PHP Automatic Loading method Collection _php Skill

Source: Internet
Author: User

PHP Loading File Mode:
1, Include,include_once,requice,requice_one normal loading
2, __autoload ()
3, Spl_autoload_register ()

General Load Mode
Suppose we have a class file a.php, which defines a class named a:

<?php  
class A {public  
  function __construct () {  
    echo ' Got it. '  
  }  
}

And then we have a index.php need to use this class A, and the usual way to do that is

<?php  
  require (' a.php ');  
  $a = new A ();

But one problem is that if our index.php needs to contain more than just class A, it needs a lot of classes, and it's going to have to write a lot of lines and require statements, and sometimes it can be frustrating.

__autoload () Auto Load
But after the PHP5 version, we don't need to do that anymore.
In php5, the __autoload function is automatically invoked when trying to use a class that is not yet defined, so we can write the __autoload function to let PHP load the class automatically without having to write a long list of included files.

For example, in the above example, index.php can write this:

<?php  
function __autoload ($class) {  
  $file = $class. '. php ';  
  if (Is_file ($file)) {  
    require_once ($file);  
  }  
}  
$a = new A ();

Of course, the above is just the simplest demonstration, __autoload just go include_path looking for class files and loading, we can define the __autoload load class rules According to our own needs. Note: Because __autoload () is a function, it can only exist once.

Spl_autoload_register () Auto Load
But now the problem is, if in a system implementation, if you need to use a lot of other class libraries, these class libraries may be written by different developers, whose class name and the actual disk file mapping rules are different. If you want to implement the automatic loading of the class library file, you must implement all the mapping rules in the __autoload () function so that the __autoload () function may be very complex or even impossible to implement. The end result may be that the __autoload () function is bloated, which can have a significant negative impact on future maintenance and system efficiency even when implemented. In this case, there is no more simple and clear solution? The answer is of course:no!

Spl_autoload_register () satisfies such requirements. It actually creates a queue of autoload functions, which are executed one at a time in the order in which they are defined. In contrast, __autoload () can only be defined once.

BOOL Spl_autoload_register ([Callable $autoload _function [, bool $throw = True [, bool $prepend = false]]]
We continue to rewrite the above example:

<?php  
function Loader ($class) {  
  $file = $class. '. php ';  
  if (Is_file ($file)) {  
    require_once ($file);  
  }  
}  
Spl_autoload_register (' loader ');  
$a = new A ();

or use anonymous functions directly:

<?php  
 
spl_autoload_register (function ($file) {
  $file = $class. '. php ';  
  if (Is_file ($file)) {  
    require_once ($file);  
  }
});  
$a = new A ();

Such a child is also able to run normally, at this time PHP is looking for a class when it does not invoke __autoload but call our own defined function loader. In the same way, the following wording is also possible:

<?php  
class Loader {public  
  static function LoadClass ($class) {  
    $file = $class. '. php ';  
    if (Is_file ($file)) {  
      require_once ($file);  
}}} Spl_autoload_register (Array (' Loader ', ' loadclass ')); 
Spl_autoload_register (Array (__class__, ' loadclass ')); 
Spl_autoload_register (Array ($this, ' loadclass ')); 
$a = new A ();

More examples
autoload.php

<?php

define (' Sdk_path ', __dir__);

Require_once Sdk_path. '/common/functions.php ';
Require_once Sdk_path. '/common/config.php ';

Spl_autoload_register (function ($class) {
  if (false!== Stripos ($class, ' yjc\wechat ')) {
    require_once __dir__. '/' . Str_replace (' \ \ ", Directory_separator, substr ($class, 10)). '. php ';
  }
};


/*
function __autoload ($class) {
  if (false!== Stripos ($class, ' yjc\wechat ')) {
    require_once __dir__. '/' . Str_replace (' \ \ ", Directory_separator, substr ($class, 10)). '. php ';
  }
*/

It is recommended that you develop the habit of defining absolute address Sdk_path in the portal file so that require will not go wrong.
The above is the entire content of this article, I hope to help you learn.

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.