Exploring the underlying PHP operating mechanism

Source: Internet
Author: User
Tags sapi
Explore the underlying operating mechanism of PHP and read and explore the underlying operating mechanism of PHP. This article describes in detail the underlying operating mechanism of PHP, including the operations of PHP content, and explained the entire lifecycle of PHP from start to stop in combination with examples. Introduction we have never started PHP-related processes manually. it runs with Apache startup. PHP uses the mod_php5.so module and Apache phase "> <LINKhref =" ht

This article explains in detail the underlying operating mechanism of PHP, including the operation of PHP content, and explains the entire lifecycle of PHP from startup to stop with examples.

Introduction

We have never started PHP-related processes manually. it runs with Apache startup. PHP is connected to Apache through the mod_php5.so module (SAPI, server Application Programming Interface );

PHP has three modules: Kernel, Zend Engine, and extension layer. PHP kernel is used to process requests, file streams, error processing, and other related operations. Zend Engine (ZE) it is used to convert the source file to the machine language and then run it on the virtual machine. the extension layer is a set of functions, class libraries, and streams, and PHP uses them to perform some specific operations. For example, we need a mysql extension to connect to the MySQL database. When ZE executes a program, it may need to connect to several extensions. Then ZE gives control to the extension and returns the control after processing specific tasks;

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

The internal running process is not that simple. 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.

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 methods 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 */
}


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 will be initialized when the RINIT of this module is called and the related content will be read;

The RINIT method can be considered as a preparation process, which 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 */
}


Step 1 of PHP shutdown

Like PHP startup, PHP shutdown is also divided into 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 */
}


Step 2 of PHP disabling

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 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 ".

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.