This article, examine how PHP code is interpreted and executed, and the life cycle of PHP scripts.
Overview
The start of the PHP service. Strictly speaking, the related process of PHP does not need to be started manually, it is running with the start of Apache. Of course, you can manually restart the PHP service if there is a need to restart the PHP service. For example, after updating the code in a formal environment with open opcode, you need to restart PHP to recompile the PHP code.
From the macro point of view, the implementation of the PHP kernel is to receive input data, internally do the corresponding processing and then output the results. For the PHP kernel, we write the PHP code is the kernel received input data, the PHP kernel receives the code data, the code we write code parsing and operation execution, and finally return the corresponding results of the operation.
However, unlike the usual C language code, to execute PHP code, the first thing to do is to "translate" the PHP code into a machine language to perform the corresponding functions. To perform the "translation" step, you need the PHP kernel: Lexical analysis, grammar analysis and other steps. Finally, the PHP kernel Zend engine is executed sequentially.
Lexical analysis
Separate the PHP code into a "unit" (TOKEN)
Syntax analysis
Convert "unit" to Zend Engine executable operation
Zend Engine Execution
Perform sequential execution of the operations performed by the syntax analysis
The beginning of all PHP programs (CGI/CLI) starts with the SAPI (Server application Programming Interface) interface. SAPI refers to the programming interface for specific PHP applications. such as Apache's mod_php.
PHP begins execution after two main stages: the start phase before processing the request and the end phase after the request.
Start stage
The entire starting phase of PHP undergoes two phases: module initialization and module activation.
Minit
This is the module initialization phase, which occurs throughout the life cycle of the Apache/nginx startup or the entire execution of a command-line program.
Rinit
module activation, which occurs at the request stage. Do some initialization work: such as registering constants, defining classes used by modules, etc.
The module implements these callback functions by following a macro when implemented:
Php_minit_function (myphpextension) {//Register constants or class initialization operations return SUCCESS;} Php_rinit_function (myphpextension) {///For example, record the request start time//And then record the end time at the end of the request. This allows us to record the time it takes to process the request return SUCCESS;}
When the PHP script request is processed, it goes to the end stage, the general script executes to the end or calls the exit or Die function, and PHP goes to the end stage.
End Stage
The end phase of PHP is divided into the deactivation module and the shutdown module two links.
Rshutdown
Deactivate module (corresponding rinit)
Mshutdown
Close module (corresponding Minit)
cli/cgi mode PHP is a single-process SAPI mode. This means that the PHP script is closed after it is executed once, and all variables and functions cannot continue to be used. That is, in CGI mode, the same PHP file variables are not available in other PHP files.
Here's an example of a single-threaded PHP SAPI life cycle.
Single Thread SAPI life cycle
Such as:
Php-f test.php
Invokes the Minit module initialization for each extension.
Request test.php
Invoke the Rinit module activation for each extension
Executive test.php
Invoke the Rshutdown deactivation module for each extension
Clean up variables and memory after executing test.php
Call the Mshutdown of each extension to close the module
Stop PHP execution
The above is a simple implementation process, the following to do some additions.
PHP will have an initialization process before invoking the module initialization for each module, including:
Initialize a number of global variables
In most cases, it is set to null.
Initialize a number of constants
The constants here are some of the constants of PHP itself.
Initializing the Zend engine and core components
The initialization operations here include memory management initialization, global use of function pointer initialization, parsing of PHP source files, parsing, assigning function pointers to intermediate code execution, initializing several hashtable (such as function tables, constant tables, etc.), preparing for INI file parsing, Prepare for PHP source file parsing, register built-in functions, standard constants, globals global variables, and more
Parsing php.ini
Read the php.ini file, set the configuration parameters, load the Zend extension, and register the PHP extension function.
Initialization of global operation functions
Initializes some global variables that are used very frequently in user space, such as: $\_get, $\_post, $\_files, and so on.
Initializing statically built modules and shared modules (Minit)
Initializes the module that is loaded by default.
Module initialization performs the operation:
Registering a module with a list of registered modules
Register the functions contained in each module with the function table
disabling functions and Classes
The Zend_disable_function function is called to remove the function represented by the disable_functions variable in the PHP configuration file from the CG (function_table) function table.
Activating the Zend Engine
Use the Init_compiler function to initialize the compiler.
Activating SAPI
Use the Sapi_activate function to initialize the SG (sapi_headers) and SG (REQUEST_INFO) and set some content for the method of the HTTP request.
Environment initialization
Initializes some environment variables that need to be used in the user control. Including server environment, request Data environment and so on.
Module Request Initialization
PHP calls the Zend_activate_modules function to traverse all modules registered in the Module_registry variable, calling its Rinit method method to implement the module's request initialization operation.
After processing the file-related content, PHP invokes php_request_startup for the request initialization operation:
Activating the Zend Engine
Activating SAPI
Environment initialization
Module Request Initialization
Code to run
Once all of the above preparations have been completed, the PHP program is executed. PHP uses Zend_compile_file to do lexical parsing, parsing, and intermediate code generation operations, returning all the intermediate code for this file. If the parsed file has a valid intermediate code generated, call Zend_excute to execute the intermediate code: These exception handlers are called if an exception occurs during execution and the user has defined the handling of these exceptions. After all the operations have been processed, PHP returns the results via eg (RETURN_VALUE_PTR_PTR).
Deactivation (Close request)
The process of closing a request for PHP is a collection of several close operations that exist in the Php_request_shutdown function. This includes:
Call all functions registered through Register_shutdown_function (). These functions, which are called at shutdown, are added in user space.
Executes all the available __destruct functions. The destructor here includes destructors for all objects in the object pool (eg (objects_store) and the destructor for each element in eg (symbol_table).
Brush out all the output.
Sends an HTTP reply header.
Destroys a variable of the global variable table (PG (http_globals)).
Close the lexical parser, parser, and intermediate code executor with the Zend_deactivate function.
Call each extension's Post-rshutdown function. Only the basic Post_deactivate_func function pointers for each extension are null.
Close the SAPI and destroy the contents of SG (sapi_headers), SG (REQUEST_INFO), etc. by Sapi_deactivate.
Closes the wrapper for the stream, closes the filter for the stream.
Turn off memory management.
Reset Maximum Execution time
End
PHP ends a process where the Sapi_flush function is called to flush out the final content. Then call the Zend_shutdown function to close the Zend engine.
Reference: [http://www.php-internals.com/book/] (http://www.php-internals.com/book/)
how PHP Works