In-depth explanation of PHP operating mechanism-PHP Tutorial

Source: Internet
Author: User
Tags php mod sapi
In-depth explanation of the PHP operating mechanism. Let's take a look at the following PHP running mechanism process: 1. we have never started PHP-related processes manually. it runs with Apache startup. 2. PHP uses mod_php5.so to simulate

Let's take a look at the following PHP running mechanism process:

1. we have never started PHP-related processes manually. it runs with Apache startup;

2. PHP is connected to Apache through the mod_php5.so module (specifically, SAPI, that is, the Server Application Programming Interface );

3. PHP has three modules: Kernel, Zend Engine, and extension layer;

4. the PHP kernel is used to process requests, file streams, error handling, and other related operations;

5. the Zend Engine (ZE) is used to convert the source file to the machine language, and then run it on the virtual machine;

6. the extension layer is a set of functions, class libraries, and streams. PHP uses them to perform certain operations. For example, we need mysql extension to connect to the MySQL database;

7. When ZE executes the program, it may need to connect to several extensions. in this case, ZE gives control to the extension and returns the control after processing the specific task;

8. finally, ZE returns the program running result to the PHP Kernel. it then delivers the result to the SAPI layer and finally outputs the result to the browser.

In-depth discussion on the PHP operating mechanism

And so on. The above process is just a simple version. let's dig deeper to see what happened behind the scenes.

After Apache is started, the PHP interpreter starts;

PHP can be started in 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 only some variable settings for the current request.

PHP running mechanism-step 1 of PHP startup 

I don't know what the first step is? Don't worry. Let's discuss it in detail.

Let's take a look at the first step, which is also the main step. Remember that the first step of operation occurs before any request arrives. After Apache is started, the PHP interpreter starts. PHP calls the MINIT method of each extension to switch these extensions to the available state. Let's see what extensions are opened in the php. ini file. MINIT means "module initialization ". Each module defines a set of functions and class libraries to process other requests.

A typical MINIT method is as follows:

PHP_MINIT_FUNCTION (extension_name) {/* Initialize functions, classes etc */}

PHP running mechanism-step 2 of PHP startup

When a page request occurs, the SAPI layer gives control to the PHP layer. Therefore, PHP sets the environment variables required to reply to this request. It also creates a variable table to store the names and values of variables generated during execution. PHP calls the RINIT method of each module, that is, "Request Initialization ". A classic example is the Session module RINIT. when the Session module is enabled in ini, the $ _ SESSION variable is initialized and the related content is read when the RINIT module is called. the RINIT method can be considered as a preparation process, it is automatically started 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 running mechanism-PHP shutdown step 1 

Like starting PHP, PHP closes the program in two steps: once the page is executed (whether it is executed at the end of the file or aborted using the exit or die function), PHP starts the cleanup program. It calls the RSHUTDOWN method of each module in sequence. RSHUTDOWN is used to clear the symbol table generated when the program is running, that is, to call 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 running mechanism: Step 2 of disabling PHP

Finally, all requests have been processed, and the SAPI is also ready to be closed. PHP starts to execute Step 2: PHP calls the MSHUTDOWN method of each extension, which is the last chance for each module to release the 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 lifecycle is over. Note that "start step 1" and "close Step 2" are performed only when the server does not have a request ".


Let's take a look at the following PHP running mechanism process: 1. we have never started PHP-related processes manually. it runs with Apache startup. 2. PHP mod through mod_php5.so...

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.