PHP extension-Lifecycle and memory management

Source: Internet
Author: User
Tags php source code phpinfo sapi variable scope

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 #definePhp_minit Zend_module_startup_n#definePhp_mshutdown Zend_module_shutdown_n#definePhp_rinit Zend_module_activate_n#definePhp_rshutdown Zend_module_deactivate_n#definePhp_minfo Zend_module_info_n#definePhp_ginit Zend_ginit#definePhp_gshutdown Zend_gshutdown#definePhp_minit_function Zend_module_startup_d#definePhp_mshutdown_function Zend_module_shutdown_d#definePhp_rinit_function zend_module_activate_d#definePhp_rshutdown_function zend_module_deactivate_d#definePhp_minfo_function Zend_module_info_d#definePhp_ginit_function zend_ginit_function#definePhp_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.

Reference article: Extension Writing part i:introduction to PHP and Zend

PHP extension-Lifecycle and memory management

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.