Home  >  Article  >  Backend Development  >  summary:PHP autoload mechanism analysis_PHP tutorial

summary:PHP autoload mechanism analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:27814browse

There are two ways to implement autoload in PHP:

1. Interceptor__autoload()

2. Set the global variable function pointer autoload_func to the specified function. Usually used in c extensions

Essentially, the former is achieved through the latter.

Analysis process, PHP5.3.6 source code

=>Zend/zend_vm_def.h Line 1894

ZEND_VM_HANDLER(109,ZEND_FETCH_CLASS,...

=>zend_execute_API.c line 1526

zend_class_entry *zend_fetch_class(const char *class_name,...

=>zend_execute_API.c line 1564

if(zend_lookup_class_ex(class_name,class_name_len,...

=>zend_execute_API.c line 1039

ZEND_API int zend_lookup_class_ex(const char *na...

=>zend_execute_API.c line 1121

retval = zend_call_function(&fcall_info, &fcall_cache TSRMLS_CC);

=>zend_execute_API.c line 758

zend_call_function

As the name suggests, the main function of zend_call_function is to call PHP functions. Its parameters fcall_info and fcall_cache point to two important structures respectively, 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 directly.

If fcall_cache.function_handler is NULL, try to find a function named fcall_info.function_name, and if it exists, execute it;

is summarized as follows,

The main execution process of the autoload mechanism is:

(1) Check whether the executor global variable function pointer autoload_func is NULL.

(2) If autoload_func is not NULL, the function pointed to by the autoload_func pointer is directly executed to load the class without checking whether the __autoload() function is defined.

(3) If autoload_func is NULL, check whether the __autoload() function is defined in the system. If it is not defined, an error is reported and exits; if the __autoload() function is defined, __autoload() is executed to try to load the class and the loading result is returned.

Automatic loading facilitates object-oriented and code reuse, but different __autoloads in multiple class libraries can cause confusion.

It can be solved with spl_autoload, which registers the interceptor functions of different developers into the hashtable of the automatic loading function.

The mechanism by which spl implements automatic loading is to maintain a hashtable, which stores various functions with automatic loading function.

When the automatic loading mechanism is triggered, zend will traverse and execute the functions in this hashtable until the class is successfully loaded or returns after the loading fails.

When you need to use the autoload function, use the function spl_autoload_register() or spl_autoload_register(autoloadfuncitonname)

Spl_autoload_register() without parameters will load the spl_autoload() function by default. This function has limited functions and can only search for the class library with the specified extension in inlcude_path.

The spl_autoload_register() with parameters no longer loads the spl_autoload() function by default.

You can view the functions in the current autoload hashtable through spl_autoload_functions(), which returns an array

Note: When using spl_autoload, the system will ignore the interceptor __autoload unless it is explicitly added to the hashtable using spl_autoload_register(__autoload)

Codefor

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478835.htmlTechArticleThere are two ways to implement autoload in PHP: 1. Interceptor __autoload() 2. Set the global variable function pointer autoload_func for the specified function. Usually used in c extensions. Essentially, the former or the latter...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn