summary:php autoload Mechanism Analysis _php Tutorial

Source: Internet
Author: User
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. Typically used in C extensions

In essence, the former is still achieved through the latter.

Analysis process, PHP5.3.6 source code

=>zend/zend_vm_def.h 1894 Lines

Zend_vm_handler (109,zend_fetch_class,...

=>ZEND_EXECUTE_API.C 1526 Lines

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

=>ZEND_EXECUTE_API.C 1564 Lines

if (ZEND_LOOKUP_CLASS_EX (Class_name,class_name_len,...

=>ZEND_EXECUTE_API.C 1039 Lines

ZEND_API int zend_lookup_class_ex (const char *na ...

=>ZEND_EXECUTE_API.C 1121 Lines

retval = Zend_call_function (&fcall_info, &fcall_cache tsrmls_cc);

=>ZEND_EXECUTE_API.C 758 Lines

Zend_call_function

As the name implies, the main function of zend_call_function is to invoke PHP functions. Its parameters fcall_info, Fcall_cache, respectively pointing to two important structures, Zend_fcall_info and Zend_fcall_info_cache

The main work flow of zend_call_function is as follows:

If Fcall_cache.function_handler is not NULL, the function that Fcall_cache.function_handler points to is executed directly.

If Fcall_cache.function_handler is null, it attempts to find a function named Fcall_info.function_name, if any, and executes it;

is summarized as follows,

The main implementation process of the autoload mechanism is:

(1) Check that the Executor global variable function pointer autoload_func is NULL.

(2) If Autoload_func is not NULL, the function directly executing the AUTOLOAD_FUNC pointer is used to load the class, and does not check whether the __autoload () function is defined.

(3) If Autoload_func is null, find out if the __autoload () function is defined in the system. If there is no definition, the error is reported and exits, and if the __autoload () function is defined, execution __autoload () attempts to load the class and returns the loading result.

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

can be resolved with Spl_autoload, the different developer's interceptor functions are registered to the hashtable of the automatic loading function.

The mechanism for automatic loading of SPL is to maintain a hashtable that stores functions with automatic loading.

When the auto-load mechanism is triggered, Zend will traverse the function that executes the Hashtable until the class is successfully loaded or the load fails to return.

Use the function spl_autoload_register () or Spl_autoload_register (autoloadfuncitonname) when you need to use the auto-load feature

The Spl_autoload () function is loaded by default for Spl_autoload_register (), which has limited functionality and can only search the class library for the specified extension in Inlcude_path.

The Spl_autoload () function is no longer loaded by default with the Spl_autoload_register () parameter.

The function in the current auto-load Hashtable can be viewed through spl_autoload_functions (), which returns an array

Note: When using spl_autoload, the system ignores the interceptor __autoload unless explicitly using Spl_autoload_register (__autoload) to add it hashtable

Codefor

http://www.bkjia.com/PHPjc/478835.html www.bkjia.com true http://www.bkjia.com/PHPjc/478835.html techarticle There are two ways to implement AutoLoad in PHP: 1, Interceptor __autoload () 2, SET global variable function pointer autoload_func to the specified function. Usually used in C extension in the former or after ...

  • 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.