A summary of the automatic loading method in PHP

Source: Internet
Author: User
This article is mainly for PHP automatic loading mode for large collection, one is the normal loading mode, the other is __autoload () automatic loading, want to know the third way please read below

PHP Load File mode:
1, include,include_once,requice,requice_one Regular 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. ';    }  }

Then we have a index.php need to use this class A, the general way of writing is

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

But one problem is that if our index.php need to include more than just class A, we need a lot of classes, and this way we have to write a lot of require statements, which sometimes makes people feel uncomfortable.

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

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

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

Of course, above is just the simplest demonstration, __autoload just go to Include_path to find the class file and load, we can define the rules of __autoload load class 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 names and the actual disk file mapping rules vary. If you want to implement the automatic loading of a class library file, you must implement all the mapping rules in the __autoload () function, so that the __autoload () function can be very complex or even impossible to implement. In the end, the __autoload () function can be bloated, and even if it can be achieved, it will have a significant negative impact on future maintenance and system efficiency. In this case, isn't there a simpler and clearer solution? The answer is of course:no!

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

BOOL Spl_autoload_register ([Callable $autoload _function [, bool $throw = True [, bool $prepend = false]]])
We 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 use anonymous functions directly:

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

This can also work properly, when PHP is looking for classes without calling __autoload instead of calling our own defined function loader. In the same way, the following 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

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

It is recommended to develop the habit of defining absolute address Sdk_path in the entry file so that require will not go wrong.

Summary: The above is the entire content of this article, I hope to be able 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.