Learn more about PHP Kernel (ii) Overview-php life cycle and Zend engine

Source: Internet
Author: User
Tags sapi
Deep understanding of the PHP Kernel (ii) Overview-php life cycle and Zend engine

This article refers to the in-depth understanding of the PHP kernel, address: Https://github.com/reeze/tipi

1. SAPI interface

SAPI (Server application programming Interface) refers to the programming interface for specific PHP applications. PHP scripts can be executed in a number of ways: through a Web server, a command line, and embedded in other programs.

While executing scripts through the Web server and the command-line program looks different, the actual process is the same. The only way the Web server is to return the results of the PHP script execution to the Web server is to print the results to the console.

The start of the script execution begins with the SAPI interface implementation, except that the different SAPI interface implementations will do their specific work, such as the Apache mod_php SAPI implementation needs to initialize some information obtained from Apache, in the output is to return the content to Apache, Other SAPI implementations are similar

2. Start and end

(1) PHP begins execution after two stages: the start phase before processing the request and the end phase after the request. The start phase has two processes: the first process is the module initialization phase (MINIT), which is performed only once during the entire SAPI life cycle (for example, the entire life cycle after Apache boot or the entire execution of a command-line program). The second process is the module activation phase (RINIT), which occurs during the request phase , such as requesting a page through a URL, and module activation before each request (Rinit request starts). For example, PHP registers a number of extension modules, then in the Minit phase callback all modules of the Minit function. Modules perform some initialization work at this stage, such as registering constants, defining classes used by modules, and so on. The module implements these callback functions by following a macro when implemented:

php_minit_function (myphpextension) {    //  Register constants or class initialization operations    return  SUCCESS;      }

The basic environment of the PHP init script after the request arrives, such as creating an execution environment, including a symbol table that holds the variable name and value contents of the PHP runtime, and a symbol table for all current functions and classes. PHP then invokes all of the module's Rinit functions, and at this stage each module can perform some related operations, and the module Rinir function is similar to the Minit callback function:

php_rinit_function (myphpextension) {    //  For example, record request    start time / / The end time is then recorded at the end of the request so that we can record the time it takes to process the request    return  SUCCESS;

After the request has been processed, it goes to the end stage, and the general script executes to the end or by calling the die () or exit () function, and PHP will go to the end stage. The end stage also has two links: one deactivates the module (rshutdown, corresponding rinit) after the request has ended, and one closes the module (Mshutdown, corresponding to Minit) at the end of the SAPI lifecycle (the Web server exits or the command line script finishes exiting).

php_rshutdown_function (myphpextension) {    //  For example, record the request end time and write the appropriate information to the log file     return  SUCCESS;}

(2) Single-process SAPI life cycle

cli/cgi mode PHP is a single-process SAPI mode. This type of request is closed after processing a request. SAPI life cycle: Start-Request Start-Request Close-end

(3) Multi-process SAPI life cycle

PHP is usually compiled into a module Apache, Apache usually use multi-process mode, Apache will fork out after the start of a number of sub-processes, each process memory space independent, each child process will go through the start and end links, However, the starting phase of each process is only done after the process fork, and it may be possible to process multiple requests throughout the lifetime of the process. The shutdown phase occurs only after the Apache is closed or the process is ended, and in these two phases the request is repeated with each request-the link to the request shutdown

(4) Multithreaded mode is similar to a process in a multi-process, but the process is repeated in parallel during the lifecycle of the request-request shutdown.

3. Zend Engine

The Zend engine is the core of the PHP implementation and provides the infrastructure for language implementations. For example, the implementation of PHP syntax, script compiler run environment, extension mechanism and memory management. PHP here refers to the official PHP implementation (as well as Facebook's hiphop, has now developed into HHVM, so far (this project) PHP does not have a standard language specification), and PHP provides request processing and other Web server interface (SAPI)

  • Related Article

    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.