This article explains in detail the underlying operating mechanism of PHP, including the operation of PHP content, and explains the entire life cycle of PHP from startup to stop with examples.
Introduction
We have never manually started the PHP related process, it runs with the startup of Apache; PHP is connected to Apache through the mod_php5.so module (specifically, SAPI, the server application programming interface);
PHP has a total of three Modules: kernel, Zend engine, and extension layer; PHP kernel is used to handle requests, file streams, error handling and other related operations; Zend engine (ZE) is used to convert source files into machine language and then run it on a virtual machine ;The extension layer is a set of functions, libraries, and streams that PHP uses to perform specific operations. For example, we need the mysql extension to connect to the MySQL database; when ZE executes the program, it may need to connect to several extensions. At this time, ZE hands over the control to the extension and returns it after processing the specific task;
finally, ZE returns the program running results. to the PHP kernel, which then transmits the results to the SAPI layer and finally outputs them to the browser.
It’s not that simpleto delve into
the inner workings of reality. 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 starts, the PHP interpreter also starts;
◆The startup process of PHP has two steps:
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 that are only specific to the current request.
first step is when starting PHP
? What are the first and second steps? Don’t worry, we’ll discuss this in detail next. Let's look at the first and most important step first. 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 starts the second step.
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
is the same as 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
, all requests have been processed and SAPI is ready to be shut down. PHP begins to execute the second step:
PHP calls the MSHUTDOWN method of each extension. This is the last opportunity 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.