Home  >  Article  >  Backend Development  >  In-depth interpretation of PHP operating mechanism_PHP tutorial

In-depth interpretation of PHP operating mechanism_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:35:121116browse

Let’s take a look at the following process of PHP operating mechanism through

:

1. We have never started it manually Through PHP related processes, it runs with the startup of Apache;

2. PHP is connected to Apache through the mod_php5.so module (specifically, SAPI, the server application programming interface);

3. PHP has three modules in total: kernel, Zend engine, and extension layer;

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

5. Zend Engine (ZE) is used to convert source files into machine language and then run it on a virtual machine;

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

7. When ZE executes the program, it may need to connect to several extensions. At this time, ZE will hand over control to the extension and return it after processing the specific task;

8. Finally, ZE returns the program execution results to the PHP kernel, which then transmits the results to the SAPI layer and finally outputs them to the browser.

Deeply explore the operating mechanism of PHP

Wait, it’s not that simple. The above process is just a simplified version, let’s dig a little deeper to see what else is going on behind the scenes.

After Apache is started, the PHP interpreter is also started;

The PHP startup process has two steps;

The first step is to initialize some environment variables, which will be It works throughout the entire SAPI life cycle;

The second step is to generate some variable settings only for the current request.

The first step of PHP startup of PHP operating mechanism  

I don’t know what the first and second steps are? Don’t worry, we’ll discuss this in detail next.

Let’s take a look at the first and most important step. The thing to remember is that the first step of the operation happens before any requests arrive. After starting Apache, the PHP interpreter also starts; PHP calls the MINIT method of each extension, thereby switching these extensions to an available state. Take a look at what extensions are opened 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:

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

PHP startup mechanism of PHP operation mechanism Step Two

When a page request occurs, the SAPI layer hands over control to the PHP layer. So PHP sets the environment variables needed to reply to this request. At the same time, it also creates a variable table to store variable names and values ​​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 will be initialized and the relevant content will be read in when the RINIT of the module is called; the RINIT method can be regarded as a The preparation process starts automatically between program executions.

A typical RINIT method is as follows:

PHP_RINIT_FUNCTION(extension_name) { /* Initialize session variables, pre-populate variables, redefine global variables etc */ }

The first step of PHP shutdown in PHP operating mechanism

Just like PHP startup, PHP shutdown is also divided into two steps: Once the page is executed (whether it reaches the end of the file or is terminated with the exit or die function ), PHP will start the cleanup process. It will call 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 on 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 */ }

The second step of PHP shutdown of PHP operating mechanism

Finally, all requests have been processed, SAPI is ready to be shut down, and PHP starts to execute the second step: PHP calls MSHUTDOWN of each extension method, this 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 should be noted that "starting the first step" and "closing the second step" will only be executed when there is no request from the server.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445939.htmlTechArticle Let’s take a look at the following PHP operating mechanism process: 1. We have never manually opened PHP related Process, which runs when Apache starts; 2. PHP is modeled through mod_php5.so...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn