Explore PHP Bottom-running mechanism _php tutorial

Source: Internet
Author: User
Tags sapi
Introduction

We have never manually opened the process of PHP, it is running with Apache boot, PHP through the mod_php5.so module and Apache connected (specifically, SAPI, the server Application programming interface);

PHP has a total of three modules: the kernel, the Zend engine, and the extension layer, the PHP kernel is used to handle requests, file streams, error handling and other related operations, the Zend Engine (ZE) to convert the source file into a machine language, and then run it on the virtual machine; the extension layer is a set of functions, class libraries, and streams. PHP uses them to perform some specific operations. For example, we need the MySQL extension to connect to the MySQL database, and when Ze executes the program it may be necessary to connect several extensions, then ze will give control to the extension, and so on after the specific task is processed and then returned;

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

In-depth discussion

The internal running process is not so 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;
There are two steps to starting the PHP process:
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 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;

PHP invokes the Minit method of each extension, which enables these 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 starts 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 into;

The Rinit method can be thought of as a preparation process that starts automatically between program execution.
A typical Rinit method is as follows:

Php_rinit_function (extension_name) {/* Initialize session variables,

Redefine Global Variables etc * *}

PHP Close First Step

Like PHP startup, PHP is closed in two steps:

Once the page has been executed (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:


unset all variables used in the last PHP call etc */}

PHP Close Second Step

Finally, all the requests have been processed and SAPI is ready to close, and PHP begins the second step:

PHP calls each extended Mshutdown method, which 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 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/446666.html www.bkjia.com true http://www.bkjia.com/PHPjc/446666.html techarticle Introduction We have never manually opened the process of PHP, it is with the launch of Apache Run, PHP through the mod_php5.so module and Apache connected (specifically, SAPI, the server application process ...)

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