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

In-depth interpretation of PHP operating mechanism_PHP tutorial

Jul 15, 2016 pm 01:35 PM
php under right turn on us go deep Interpretation process Operating mechanism this pass

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles