Example analysis of autoload operation mechanism of PHP

  • 2021-07-16 02:03:09
  • OfStack

In this paper, the operation mechanism of autoload of PHP is deeply analyzed. It is helpful to deeply understand the operation principle of PHP. The specific analysis is as follows:

php There are two ways to implement autoload:

1. Interceptor __autoload ()

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

In essence, the former is realized through the latter.

Analysis process, PHP 5.3. 6 source code:

= > Line Zend/zend_vm_def. h 1894
ZEND_VM_HANDLER(109,ZEND_FETCH_CLASS,...
= > Line zend_execute_API. c 1526
zend_class_entry *zend_fetch_class(const char *class_name,...
= > Line zend_execute_API. c 1564
if(zend_lookup_class_ex(class_name,class_name_len,...
= > Line zend_execute_API. c 1039
ZEND_API int zend_lookup_class_ex(const char *na...
= > Line zend_execute_API. c 1121
retval = zend_call_function( & fcall_info, & fcall_cache TSRMLS_CC);
= > Line zend_execute_API. c 758
zend_call_function

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

The main workflow of zend_call_function is as follows:

If fcall_cache.function_handler is not NULL, the function pointed to by fcall_cache.function_handler is executed directly.
If fcall_cache.function_handler is NULL, try to find a function named fcall_info.function_name and execute it if it exists;

Summed up as follows, the main implementation process of 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 executed directly to load the class without checking whether the __autoload () function is defined.
(3) If autoload_func is NULL, find out if the __autoload () function is defined in the system. If it is not defined, report an error and exit; If the __autoload () function is defined, then __autoload () attempts to load the class and returns the loading result.

Auto-loading facilitates object-oriented and code reuse, but __autoload with multiple different class libraries can lead to confusion. It can be solved with spl_autoload, and interceptor functions of different developers are registered in hashtable of automatic loading function. The mechanism of spl to realize automatic loading is to maintain an hashtable, which stores various functions with automatic loading function.

When the auto-load mechanism is triggered, the zend iterates through the functions in the hashtable until the class is successfully loaded or the load fails.
Use the functions spl_autoload_register () or spl_autoload_register ('autoloadfuncitonname') when auto-load is required
The parameterless spl_autoload_register () loads the spl_autoload () function by default, which is limited to searching for class libraries with the specified extension in inlcude_path.

The spl_autoload_register () with parameters no longer loads the spl_autoload () function by default.
spl_autoload_functions () can be used to view the function currently automatically loaded in hashtable, which returns an array

Note: When using spl_autoload, the interceptor __autoload is ignored unless explicitly added to hashtable using spl_autoload_register ('__autoload')

I hope this paper is helpful to everyone's PHP program design


Related articles: