PHP kernel exploration: One-time request life cycle

Source: Internet
Author: User
Tags sapi zend

We have never manually opened a PHP-related process, which is running with Apache booting. PHP is connected to Apache via the Mod_php5.so module (specifically, SAPI, the server Application programming interface).

PHP has a total of three modules: the kernel, the Zend engine, and the extension layer.

    • The PHP kernel is used to handle requests, file streams, error handling and other related operations;
    • The Zend Engine (ZE) is used to convert the source file into a machine language and then run it on the virtual machine;
    • An extension layer is a set of functions, class libraries, and streams that PHP uses to perform certain operations.

For example, we need the MySQL extension to connect to the MySQL database, and when the Ze executes the program it may be necessary to connect several extensions, then ze gives control to the extension, and then returns after the specific task is processed, and finally ze the program run results to the PHP kernel, which then transmits the results to the SAPI layer. The final output to the browser.

In-depth discussion

The real internal running process is not so simple. The above process is just a brief version, let's dig deeper and see what else is happening behind the scenes.

After Apache starts, the PHP interpreter also starts. There are two steps to starting the PHP process:

    • The first step is to initialize some environment variables, which will take effect throughout the SAPI life cycle;
    • The second step is to generate some variable settings for the current request only.
PHP Start the first step

Not sure what the first second step is? Don't worry, we'll discuss it in detail next. Let's take a look at the first step, but also the most important step. Keep in mind that the first step of the operation occurs before any requests arrive.

When Apache is started, the PHP interpreter is started. PHP invokes the Minit method of each extension, which enables these extensions to switch to the available state. See what extensions are open in the php.ini file. Minit means "module initialization". Each module defines a set of functions, class libraries, and so on to handle other requests.

A typical Minit method is as follows:

Php_minit_function (extension_name) {/* Initialize functions, classes etc *}
PHP starts the second step

When a page request occurs, the SAPI layer takes control over to the PHP layer. PHP then sets the environment variables that are required to respond to this request. It also creates a variable table that holds the variable names and values that are generated during execution. PHP calls the Rinit method of each module, which is "request initialization". A classic example is the Rinit of the session module, if the session module is enabled in PHP.ini, the $_session variable is initialized when the module is called Rinit, and the relevant content is read; The Rinit method can be thought of as a preparation process, It starts automatically between program execution. A typical Rinit method is as follows:

Php_rinit_function (extension_name) {/* Initialize session Variables,pre-populate variables, redefine global variables ETC */}
PHP Close First Step

Like PHP startup, PHP is closed in two steps. Once the page has been executed (either at the end of the file or with the exit or Die function aborted), PHP starts the cleanup program. It invokes the Rshutdown method of each module sequentially. Rshutdown is used to clear the symbol table generated by the program runtime, which is called the unset function for each variable.

A typical Rshutdown method is as follows:

Php_rshutdown_function (extension_name) {/* do memory management, unset all variables used in the last PHP call etc */}
PHP Close Second Step

Finally, all the requests have been processed and SAPI is ready to shut down, and PHP begins the second step: PHP calls each extended Mshutdown method, which is the last chance for each module to release memory.

A typical Rshutdown method is as follows:

Php_mshutdown_function (extension_name) {/* Free handlers and persistent memory etc *}

In this way, the entire PHP life cycle is over. It is important to note that the "Start first step" and "close second step" are performed only if the server does not have a request.

PHP kernel exploration: One-time request life cycle

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.