PHP autoload Running Mechanism instance analysis and autoload instance analysis

Source: Internet
Author: User
Tags spl

PHP autoload Running Mechanism instance analysis and autoload instance analysis

This article provides an in-depth analysis of the PHP autoload operating mechanism. It is helpful for you to thoroughly understand the PHP operating principles. The specific analysis is as follows:

Php implements autoload in two ways:

1. interceptor _ autoload ()

2. Set the global variable function pointer autoload_func as the specified function. Usually used in c Extension

In essence, the former is implemented through the latter.

Analysis process, PHP5.3.6 source code:

=> Zend/zend_vm_def.h, row 1894
ZEND_VM_HANDLER (109, ZEND_FETCH_CLASS ,...
=> Zend_execute_API.c, row 1526
Zend_class_entry * zend_fetch_class (const char * class_name ,...
=> Zend_execute_API.c, row 1564
If (zend_lookup_class_ex (class_name, class_name_len ,...
=> Zend_execute_API.c, row 1039
ZEND_API int zend_lookup_class_ex (const char * na...
=> Zend_execute_API.c, row 1121
Retval = zend_call_function (& fcall_info, & fcall_cache TSRMLS_CC );
=> Zend_execute_API.c, row 758
Zend_call_function

As the name suggests, the main function of zend_call_function is to call PHP functions. The fcall_info and fcall_cache parameters respectively point to two important structures: zend_fcall_info and zend_fcall_info_cache.

The main workflow of zend_call_function is as follows:

If fcall_cache.function_handler is not NULL, execute the function pointed to by fcall_cache.function_handler.
If fcall_cache.function_handler is NULL, try to find the number of functions named fcall_info.function_name. If so, execute it;
 
The main execution process of the autoload mechanism is as follows:

(1) check whether the global variable function pointer autoload_func of the executor is NULL.
(2) If autoload_func is not NULL, directly execute the autoload_func pointer to the function to load the class and check whether the _ autoload () function is defined.
(3) If autoload_func is NULL, check whether the _ autoload () function is defined in the system. If no value is defined, the system reports an error and exits. If the _ autoload () function is defined, the system executes _ autoload () to load the class and return the loading result.

Automatic Loading facilitates object-oriented and code reuse, but different _ autoload libraries can cause confusion. You can use spl_autoload to register the interceptor functions of different developers to the hashtable that automatically loads the functions. The automatic loading mechanism of spl is to maintain a hashtable, which stores various functions with the automatic loading function.

When the automatic loading mechanism is triggered, zend will traverse and execute the functions in the hashtable until the class is loaded successfully or the loading fails, and then return.
To use the automatic loading function, use the spl_autoload_register () or spl_autoload_register ('autoloadfuncitonname') function ')
Spl_autoload_register () without parameters loads the spl_autoload () function by default. This function has limited functions and can only search for class libraries with the specified extension in inlcude_path.

By default, the spl_autoload_register () function is no longer loaded.
You can use spl_autoload_functions () to view the functions automatically loaded in hashtable. This function returns an array.

Note:When using spl_autoload, the system will ignore the interceptor _ autoload, unless explicitly using spl_autoload_register ('_ autoload') to add it to hashtable

I hope this article will help you with PHP programming.


Loading of _ autoload () in php

Share with you what I'm using
// Auto mount class function AutoLoad ($ classname) {// class $ filepath = BASE_CLASS. $ classname. '. class. php '; if (file_exists ($ filepath) {return include $ filepath;} // Library File $ filepath = BASE_LIB. $ classname. '. lib. php '; if (file_exists ($ filepath) {return include $ filepath;} spl_autoload_register ('autoload'); usage:
// Check the logon User: is_loginin (); // automatically instantiate the User and execute is_loginin

Please kindly explain the _ autoload function and the spl_autoload_register () function in PHP. I have not found any information on the Internet.

_ Autoload is often used for processing when the class library is automatically loaded
That is, the method described on the internet, locate the class file based on the class name, and then require_one
Spl_autoload_register ()

_ The biggest defect of autoload is that there cannot be multiple autoload Methods

Now, let's look at the following scenario. Your project references another project. Your project has a _ autoload, and other projects have a _ autoload, in this way, the two _ autoloads conflict. The solution is to change _ autoload into one, which is undoubtedly very tedious.

Therefore, we urgently need to use an autoload call stack, so that the spl autoload series functions will appear. You can use spl_autoload_register to register multiple custom autoload functions.

If your PHP version is later than 5.1, you can use spl_autoload

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.