An in-depth explanation of PHP operating mechanism _php tutorial

Source: Internet
Author: User
Tags sapi
Everyone through the

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

1. We have never manually opened the relevant process of PHP, it is running with the start of Apache;

2. PHP is connected to Apache via the Mod_php5.so module (specifically, SAPI, the server Application programming interface);

3. PHP has a total of three modules: the kernel, the Zend engine, and the expansion layer;

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

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

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

7. When Ze executes the program, it may be necessary to connect a number of extensions, then Ze will control to the extension, and so on after the specific tasks are processed and then returned;

8. Finally, ZE returns the result of the program run to the PHP kernel, which then transmits the results to the SAPI layer, which is eventually output to the browser.

In-depth discussion of PHP operating mechanism

Wait, it's not that simple. The above process is just a brief version, let's dig deeper and see what else is happening behind the scenes.

After Apache starts, the PHP interpreter program also starts;

PHP Start-up process 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 for the current request only.

PHP running mechanism PHP start the first step 

Not sure what the first second step is? Don't worry, we'll discuss it in detail next.

Let's take a look at the first step, but also the most important step. Keep in mind that the first step of the operation occurs before any requests arrive. After starting Apache, the PHP interpreter also starts, and PHP invokes the Minit method of each extension, which allows the extensions to switch to the available state. See what extensions are open in the php.ini file, and Minit means "module initialization." Each module defines a set of functions, class libraries, and so on to handle other requests.

A typical Minit method is as follows:

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

PHP running mechanism of PHP start the second step

When a page request occurs, the SAPI layer takes control over to the PHP layer. PHP then sets the environment variables that are required to respond to this request. It also creates a variable table that holds the variable names and values that are 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 is initialized when the module is called Rinit, and the relevant content is read; The Rinit method can be thought of as a preparation process, It starts automatically between program execution.

A typical Rinit method is as follows:

Php_rinit_function (extension_name) {/* Initialize session variables, pre-populate variables, redefine global variables E TC */}

PHP run mechanism PHP close first step 

Like PHP startup, PHP is closed in two steps: Once the page executes (either at the end of the file or with the exit or Die function aborted), PHP starts the cleanup program. It invokes the Rshutdown method of each module sequentially. Rshutdown is used to clear the symbol table generated by the program runtime, which is called 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 run mechanism PHP close second step

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

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

In this way, the entire PHP life cycle is over. It is important to note that the "Start first step" and "close second step" are performed only if the server does not have a request.


http://www.bkjia.com/PHPjc/445939.html www.bkjia.com true http://www.bkjia.com/PHPjc/445939.html techarticle Everyone through the first look at the following PHP operating mechanism of the process: 1. We have never manually opened a PHP related process, it is running with Apache boot; 2. PHP via mod_php5.so ...

  • Related Article

    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.