PHP autoload running mechanism example analysis, autoload example analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:19:58
Original
758 people have browsed it

PHP autoload operating mechanism example analysis, autoload example analysis

This article provides a more in-depth analysis of PHP’s autoload operating mechanism. It is helpful to deeply understand the operating principles of PHP. The specific analysis is as follows:

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;

The summary is 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 of spl to implement 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')

I hope this article will be helpful to everyone’s PHP programming

Loading problem with __autoload() in php

Let me share with you what I am using
//Automatically mount class function AutoLoad($classname){ // class $filepath = BASE_CLASS . $classname . '.class.php'; if (file_exists ($filepath)) { return include $filepath; } //lib library file $filepath = BASE_LIB . $classname . '.lib.php'; if (file_exists($filepath)) { return include $filepath; }}spl_autoload_register( 'AutoLoad'); Usage:
//Check login User::is_loginin();//Automatically instantiate User and execute is_loginin

Urgent, could you please explain the __autoload function and spl_autoload_register() function in PHP? I looked for information on the Internet and couldn’t understand it.

__autoload is commonly used in automatic loading of class libraries
This is the method mentioned on the Internet. According to the class name, find the class file, and then require_one
spl_autoload_register()

The biggest flaw of __autoload is that it cannot Multiple autoload methods

Okay, think about the following scenario. Your project references a project of someone else. There is an __autoload in your project, and another person’s project also has an __autoload, so there are two __autoload conflicts. The solution is to modify __autoload to become one, which is undoubtedly very cumbersome.

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871095.htmlTechArticleAn example analysis of PHP’s autoload operating mechanism. Autoload example analysis This article provides a more in-depth analysis of PHP’s autoload operating mechanism. It is helpful to deeply understand the operating principles of PHP. Tool...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!