1. PHP Source code structure
PHP has two kernel subsystems, ZE (Zend Engine) and PHP core.
Ze is responsible for parsing the PHP script into machine code (also becomes the token character), executes these machine code in the process space, Ze also is responsible for the memory management, the variable scope management and the PHP function dispatch management.
PHP Core is responsible for communication with SAPI layer, PHP core also provides a unified control layer for Safe_mode, open_basedir check, and PHP core provides streams layer for file and network IO operation of user domain. where SAPI (Server application programming Interface) typically contains host environments such as nginx,apache,iis,cli,cgi.
PHP extensions provide encapsulation of common operations on Ze and PHP core, such as reading and writing to Mysql,redis,memcache,sqlite, parsing json,xml files, encapsulation of Soap,sokcet,curl network protocols, The encapsulation of encryption, decryption and decompression, the encapsulation of image processing and so on. Some extensions implement a feature from scratch, such as using C for REDIS communication protocols to communicate with Redis, and some extensions by invoking the system's existing libraries, which require the system itself to have a corresponding GD library installed.
78 extensions are available in PHP source Php-5.6.24/ext.
In summary, the infrastructure provided by Ze and PHP core provides a variety of operations for the user domain by EXT (extended).
Take php-5.6.24 source code as an example, ze corresponding folder php-5.6.24/zend, PHP core corresponding folder Php-5.6.24/main, expand the corresponding folder Php-5.6.24/ext.
2. php Extension life cycle
When PHP receives the SAPI command, it initializes and starts its kernel subsystem, and at the end of the boot of the kernel subsystem, PHP begins to load its extension code and initialize the extension, at which time PHP invokes the initialization routines module of each module initialization Routine (Minit).
Minit (Module initialization)
PHP calls Minit related routines so that each extension has the opportunity to initialize internal variables, allocate resources, register resource handling handles, and register its own functions with ze so that the script calls this function ze know what code to execute
Rinit (Request initialization)
After the module initialization is complete, PHP waits for a request from SAPI, and when the SAPI request is received, the ze creates the runtime environment for the currently requested PHP script and invokes each extended request initialization (Rinit) function. Each extension has the opportunity to set specific environment variables, allocate resources on request, or perform other tasks, such as auditing.
The SAPI requests are divided into two categories, Apache, IIS, and other mature Web server Sapis, which PHP executes minit at startup, waits for page requests from the user, and executes rinit when the request is received. Another type of SAPI request is the CGI or CLI sapis,php, when it receives such SAPI requests, executes the minit immediately after execution rinit.
When the Rinit request is initialized, Ze takes control and translates the currently requested script into tokens, which ultimately constitutes opcodes (opcode), opcodes is executed, and if a opcode requires execution of an extension function, This is ze will bind the relevant parameters to the modified function, and give control temporarily to the function to execute, until the function is finished executing.
Rshutdown (Request Shutdown)
After the PHP script runs, PHP calls each extended request-close (Rshutdown) function to perform the final cleanup work (such as saving the session variable to disk). Next, Ze performs the cleanup process (garbage collection), effectively executing unset () for each variable used during the previous request.
Mshutdown (Module Shutdown)
When the Rshutdown is complete, PHP continues to wait for other document requests from SAPI or to turn off the signal. For SAPI such as CGI and CLI, there is no "next request", so SAPI immediately begins to shut down. During shutdown, PHP iterates through each extension again, invokes its module shutdown (mshutdown) function, and eventually shuts down its own kernel subsystem.
Ginit
Initializing global variables
Gshutdown
Releasing global variables
MINFO
Set the information for the Phpinfo module, phpinfo the configuration information to rank each extension
Main/php.h #define PHP_MINIT zend_module_startup_n #define Php_mshutdown zend_module_shutdown_n #define Php_rinit zend_module_activate_n #define Php_rshutdown zend_module_deactivate_n #define PHP_MINFO ZEND _module_info_n #define PHP_GINIT zend_ginit #define Php_gshutdown zend_gshutdown #define Php_minit_function zend_module_startup_d #define Php_mshutdown_function Zend_module_shutdown_d #define Php_rinit_function zend_module_activate_d #define Php_rshutdown_function Zend_module_deactivate_d #define Php_minfo_function zend_module_info_d #define Php_ginit_function ZEND_ Ginit_function #define Php_gshutdown_function zend_gshutdown_function
3. Memory Management for PHP extensions
Ze performs its own internal memory management by attaching flags to identify if a memory variable is persistent and, for non-persistent memory, ze cleans it. However, it is best to clean up non-persistent memory in the extension itself, because the extension of the non-persistent memory that it requests to allocate will remain in the state for an extended period of time, so that the resources associated with it are not released for a long time.