Analysis of php-automatic loading principle

Source: Internet
Author: User
Tags autoload spl

Speaking of PHP automatic loading, many students may think of various framework of the automatic loading function, PHP specifications in the PSR0 and PSR4 principles, composer Automatic loading function, and so on, these are for our development to provide a great convenience.

So what are the causes and consequences of automatic PHP loading? What is the internal principle of PHP? Next, I will analyze and summarize according to my own understanding:

Why does it have automatic loading?

In PHP oo programming, in order to facilitate management, we will write a class in a separate file, then if you want to use Class B in Class A function, you need to load Class B into Class A. For such requirements in the most primitive time, we are implemented through require and include syntax, these 2 kinds of grammatical results are basically the same, there are some differences in the execution process, not explained here. For example:

File b.php<?phpClassb{PublicfunctionEcho_info(){Echo"I am the result of method execution in class B"; }}?>File a.php<?phprequire  ' b.php '; Span class= "hljs-comment" >//include ' b.php '; class a{ public function test () { $b _object =  New B ();  $b _object->echo_info ();}}  $a _object = new A (); ?> command line Input:  #php a.php output: "I am class b Method Execution Result "       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

As a result, PHP5 implements the auto-load (Autoload) function of the class, which was originally implemented by a magic method of PHP __autoload (). Later, PHP's extended SPL (Standard PHP Library) has implemented a more powerful automatic loading mechanism.

PHP Original Automatic loading

First, let's introduce the next __autoload () method. Or just for example, use __autoload () to make the following changes:

File b.php do not modifyFile a.php<?phpClassa{Publicfunctiontest () { $b _object = new B ();  $b _object->echo_info ();}} function __autoload ( $classname) {require  $classname. //include ' b.php ';}  $a _object = new A (); ?> command line Input:  #php a.php output: "I am class b Method Execution Result "       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

We add a function in a file: __autoload (), and we write the corresponding introduction method in the function, after the operation also obtained the result, no error. We need to make clear that the __autoload () function will be executed automatically when the class is not found, but PHP does not define the function internally, it needs to develop its own definition and write internal logic, and PHP is only responsible for automatically calling execution when needed. And when the call is automatically transmitted to the class to be loaded as a parameter.

With the __autoload () function, it can be seen that if we need to introduce 100 other files now, we just need to set a rule and write a function. This is much better than direct use of require/inlude, but there are also new problems, in a project, we can only write a __autoload () function, if the project is relatively large, loading each file using the same rules is obviously unrealistic, then we may need in __ AutoLoad () writes complex rule logic to satisfy the need to load different files. This also makes the __autoload () function more complex and difficult to maintain management.

As a result, the automatic loading mechanism of SPL (standard PHP library) is born.

SPL Auto Load

First, make it clear that PHP, when instantiating an object (actually implementing an interface, using a static variable in a class constant or class, invokes a static method in a class), first finds out whether the class (or interface) exists in the system, and attempts to load the class using the autoload mechanism if it does not exist. The main implementation process of the autoload mechanism is:

    • Checks if the executor global variable function pointer autoload_func is null;
    • If Autoload_func==null, the lookup system defines whether the __autoload () function is defined and, if defined, executes and returns the loaded result. If there is no definition, the error and exit;
    • If Autoload_func is not equal to NULL, the function load class that Autoload_func points to is executed directly, and the __autoload () function is not checked for definition.

By understanding the PHP automatic loading process, you can see that PHP actually provides two ways to implement the automatic loading mechanism:

    • A user-defined __autoload () function, which we have mentioned earlier, is usually implemented in PHP source programs;
    • Another is to design a function that points the autoload_func pointer to it, which is usually implemented in the PHP extension using the C language, the SPL autoload mechanism.

If both methods are implemented, that is, autoload_func is not equal to NULL, the program executes only the second way, and the __autoload () function is not executed.

Let's look at a SPL auto-load Example:

b.php file is not changed a.php<?phpClassa{PublicfunctionTest(){$b _object =New B ();$b _object->echo_info (); }}function__autoload($classname) {Require $classname. //include ' b.php ';} function my_autoload ( $classname) {require  $classname. //include ' b.php '; echo  ' my_autoload ';} Spl_autoload_register ( ' my_autoload '); new A (); class B;            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25

In this small example, you can see that by Spl_autoload_register (' My_autoload '), you implement a custom My_autoload () function that loads Class B when the program execution cannot find Class B. The effect of Spl_autoload_register (' my_autoload ') is actually to point the Autoload_func pointer to My_autoload (). Now, the entire PHP automatic loading process is understood.

Next we analyze the whole process of the automatic loading of SPL in detail.

First, or just a small example, if the Spl_autoload_register (' My_autoload ') changed to Spl_autoload_register () do not add any parameters, Class B can be loaded? The answer is: yes.
Why is it?

Since the SPL extension internally defines an auto-load function spl_autoload (), it implements the function of auto-loading, if we do not define our own auto-loading function, and the program writes Spl_autoload_register () (if no arguments are passed, Must be executed for the first time) or Spl_autoload_register (' spl_autoload '), then the Autoload_func pointer will point to the intrinsic function spl_autoload (). When the program executes, the auto-load function executes if the corresponding class is not found.

So, how does SPL implement a autoload_func pointer pointing to a different function?

Originally, a function Spl_autoload_call () and a global variable autoload_functions were defined within SPL. Autoload_functions is essentially a hashtable, but we can simply consider it as a linked list, and each element in the list is a function pointer to a function that has the automatic load class function.

The function of Spl_autoload_call () is to traverse autoload_functions sequentially, so that autoload_func points to each auto-load function, and if the load succeeds, it stops, and if it does not succeed, it continues to traverse the next auto-load function. Until the load succeeds or all functions are traversed.

So, who does this list of autoload_functions to maintain? is the Spl_autoload_register () function. The registration of the auto-loading function We are talking about is actually adding the auto-load function to the Autoload_functions list by Spl_autoload_register ().

So far, the entire automatic loading process is the end of the analysis.

  相关SPL自动加载函数:  spl_autoload_functions() //打印autoload_functions列表  spl_autoload_unregister() //注销自动加载函数
    • 1
    • 2
    • 3
    • 4

Reference resources:
Http://www.jb51.net/article/31279.htm

Analysis of php-automatic loading principle

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.