Php automatic loading method set

Source: Internet
Author: User
This article mainly targets large sets of php automatic loading methods. one is the conventional loading mode, and the other is the _ autoload () automatic loading, for more information about the third method, see the following php file loading method:
1. include, include_once, requice, and requice_one are loaded normally.
2. _ autoload ()
3. spl_autoload_register ()

Normal loading mode
Suppose we have A class file named A. php, which defines A class named:

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

Then we have an index. php that needs to use this Class A. The General syntax is

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

However, there is a problem, for example, our index. php needs to include not only Class A, but many classes. in this way, many lines of require statements must be written, which sometimes makes people feel uncomfortable.

_ Autoload () automatic loading
However, in versions later than php5, we no longer need to do this.
In php5, The _ autoload function is automatically called when you try to use a class that has not yet been defined. Therefore, you can compile the _ autoload function to allow php to automatically load the class, instead of writing a long list of contained files.

For example, in the above example, index. php can be written as follows:

<?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 example, __autoload just goes to include_path to find the class file and load it. we can define the _ autoload loading class rules as needed. Note: Because _ autoload () is a function, it can only exist once.

Spl_autoload_register () automatic loading
But now the problem arises. if many other class libraries need to be used in the implementation of a system, these class libraries may be compiled by different developers, the ing rules for class names and actual disk files are different. To automatically load the class library file, you must implement all the ing rules in the _ autoload () function. in this case, _ autoload () functions may be very complex and cannot be implemented. In the end, the _ autoload () function may be very bloated. even if it can be implemented, it will bring a huge negative impact to future maintenance and system efficiency. In this case, isn't there a simpler and clearer solution? The answer is:NO!

Spl_autoload_register () meets these requirements. It actually creates a queue for the autoload function and runs it one by one in the defined order. In contrast, _ autoload () can be defined only once.

Bool spl_autoload_register ([callable $ autoload_function [, bool $ throw = true [, bool $ prepend = false])
We will continue to rewrite the example above:

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

Or directly use the anonymous function:

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

This way, php can run normally. at this time, when looking for classes, php does not call _ autoload, but calls our own function loader. In the same way, the following statements can also be used:

<?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

<?phpdefine('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';  }}*/

We recommend that you create the habit of defining the absolute address SDK_PATH in the entry file so that the require will not go wrong.
The above is all the content of this article, hoping 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.