PHP - Manual: Life cycle of an extension
2024-12-23
A PHP extension goes through several phases during its lifetime. All of these phases are opportunities for the developer to perform various initialization, termination, or informational functions. The Zend API allows for hooks into five separate phases of an extension's existence, apart from calls by PHP functions.
As the Zend engine runs, it processes one or more "requests" from its client. In the traditional CGI implementation, this corresponds to one execution of a process. However, many other implementations, most notably the Apache module, can map many requests onto a single PHP process. A PHP extension may thus see many requests in its lifetime.
zend_module
structure as it is loaded.
zend_module
structure.
zend_module
structure.
zend_module
structure at this time.
There are many tasks that might be performed at any of these four points. This table details where many common initialization and termination tasks belong.
Module initialization/termination | Request initialization/termination |
---|---|
Allocate/deallocate and initialize module global variables | Allocate/deallocate and initialize request-specific variables |
Register/unregister class entries | |
Register/unregister INI entries | |
Register constants |
Aside from globals initialization and certain rarely-used callbacks, there is one more part of a module's lifecycle to examine: A call to phpinfo(). The output a user sees from this call, whether text or HTML or anything else, is generated by each individual extension that is loaded into the PHP interpreter at the time the call is made.
To provide for format-neutral output, the header "ext/standard/info.h" provides an array of functions to produce standardized display elements. Specifically, several functions which create the familiar tables exist:
Using these four functions, it is possible to produce status information for nearly any combination of features in an extension. Here is the information callback from the counter extension:
Example #1 counter's PHP_MINFO function
/* {{{ PHP_MINFO(counter) */ PHP_MINFO_FUNCTION(counter) { char buf[10]; php_info_print_table_start(); php_info_print_table_row(2, "counter support", "enabled"); snprintf(buf, sizeof(buf), "%ld", COUNTER_G(basic_counter_value)); php_info_print_table_row(2, "Basic counter value", buf); php_info_print_table_end(); } /* }}} */
官方地址:https://www.php.net/manual/en/internals2.structure.lifecycle.php