PHP once request life cycle ____php

Source: Internet
Author: User

Original: http://www.nowamagic.net/librarys/veda/detail/1287

We have never manually opened the related process of PHP, it is running with the launch of Apache. 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 expansion 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 machine language and then run it on the virtual machine; the extension layer is a set of functions, class libraries, and streams that PHP uses to perform certain actions.

For example, we need MySQL extensions to connect to the MySQL database; when Ze executes the program, it may need to connect several extensions, and Ze will give control to the extension, and so on, and then return it after processing a particular task. Finally, ZE returns the results of the program to the PHP kernel, which sends the results to the SAPI Final output to the browser. In-depth Discussion

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

After Apache started, the PHP interpreter also started. PHP starts with two steps: The first step is to initialize some environment variables, which will take effect throughout the SAPI lifecycle; the second step is to generate some variable settings that are only for the current request. PHP Start the first step

It's not clear what the first and second steps are. Don't worry, we'll discuss it in detail next. Let's take a look at the first step and the most important step. Keep in mind that the first step occurs before any request arrives.

When you start Apache, the PHP interpreter starts. PHP invokes the Minit method of each extension to switch these extensions 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, etc. to handle other requests.

A typical Minit method is as follows:

1 Php_minit_function (extension_name) {/* Initialize functions, classes etc * * *}
php start Step two

When a page request occurs, the SAPI layer controls the control over the PHP layer. PHP then sets the environment variables needed to reply to this request. It also creates a variable table that holds the variable names and values that are generated during execution. PHP invokes the Rinit method of each module, that is, "request initialization." A classic example is the Rinit of the session module, which, if a session module is enabled in PHP.ini, initializes the $_session variable when the rinit of the module is invoked and reads the relevant content; The Rinit method can be seen as a preparation process, Will start automatically between execution of the program. A typical Rinit method is as follows:

1 Php_rinit_function (extension_name) {/* Initialize session Variables,pre-populate variables, redefine global variables ETC */}
PHP Close the first step

Like PHP startup, PHP is closed in two steps. Once the page has been executed (whether it was executed to the end of the file or aborted with the exit or Die function), PHP starts the cleanup process. It invokes the Rshutdown method of each module in order. Rshutdown is used to clear the symbol table that is generated when the program is run, that is, to call the unset function on each variable.

A typical Rshutdown method is as follows:

1 Php_rshutdown_function (extension_name) {/* do memory management, unset all variables used at the last PHP call etc/}
PHP Closes the second step

Finally, all the requests have been processed and SAPI is ready to close, 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:

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

In this way, the entire PHP lifecycle is over. Note that the start first step and close second step are performed only if the server does not request it.

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.