Home > Backend Development > PHP Tutorial > What is the composition of PHP? What is the life cycle?

What is the composition of PHP? What is the life cycle?

慕斯
Release: 2023-04-10 08:30:01
forward
4528 people have browsed it

This article will introduce to you what is the composition of PHP? What is the life cycle? . It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is the composition of PHP? What is the life cycle?

## 1. PHP open source source code download address:

https://github.com/php/php-src.git

2. The structure of PHP

1. Directory structure

2. Directory analysis

(1) The sapi directory is the application interface layer of PHP.

(2) Main is the main code of PHP, mainly input/output, Web communication, initialization operations of the PHP framework, etc., such as fastcgi protocol parsing, extension loading, PHP configuration parsing and other tasks. Done here.

(3) The Zend directory is the main implementation of the PHP parser, namely ZendVM, which is the core implementation of the PHP language. The interpretation and execution of PHP code are completed by Zend.

(4) The ext directory is the extension directory of PHP, such as PHP's curl extension, gd library extension, pdo extension, etc. are all here.

(5) The TSRM directory is an implementation related to thread safety.

(6) Relationship diagram between the various components of PHP:

3, SAPI

(1) PHP is a script parser that provides script parsing and execution. Its input is ordinary text, which is then parsed and executed by the PHP parser according to predefined grammar rules. We can apply this parser in different environments, such as the command line, the Web environment, and embedding it in other applications. To this end, PHP provides a SAPI layer to adapt to different application environments. SAPI can be considered as the host environment of PHP.

(2) SAPI is also the outermost part of the entire PHP framework. It is mainly responsible for the initialization of the PHP framework. The SAPI code is located in the /sapi directory of the PHP source code. The two frequently used SAPIs are Cli and Fpm.

4. ZendVM

(1) ZendVM is a virtual computer, which is between PHP applications and actual computers. The PHP code we write is interpreted by it implemented. ZendVM is the core implementation of the PHP language. It mainly consists of two parts: compiler and executor.

(2) The compiler is responsible for interpreting PHP into instructions that can be recognized by the executor, and the executor is responsible for executing the instructions interpreted by the compiler.

(3) The role of ZendVM is equivalent to the JVM in Java. They are both abstract virtual computers.

5. Extension

(1) Extension is a set of methods provided by the PHP kernel to expand PHP functions. There are a wealth of extensions available in the PHP community Used, these extensions provide PHP with a large number of practical functions. Many operating functions in PHP are provided through extensions.

(2) Through extension, we can use C/C to achieve more powerful functions and higher performance. Extensions are divided into PHP extensions and Zend extensions. PHP extensions are more common, while Zend extensions are mainly used in ZendVM. The well-known Opcache is the Zend extension.

3. PHP life cycle

1. The entire life cycle of PHP is divided into the following stages:

(1) Module initialization stage (module startup)

(2) Request initialization phase (request startup)

(3) Script execution phase (execute script)

(4) Request closure phase (request shutdown)

(5) Module shutdown phase (module shutdown)

Note: According to different SAPI implementations, there will be some differences in the execution of each stage. For example, in command line mode, each time Executing a script will go through these stages. In FastCgi mode, module initialization is performed once at startup, and then each request only goes through the request initialization, script execution, and request closing stages. When SAPI is closed, it only goes through the module closing stage.

2. Module Initialization Phase—Analysis

(1) This phase mainly carries out the initialization operations of the PHP framework and Zend engine. The entry function of this stage is php_module_startup(). This stage is generally only executed once when SAPI starts. For Fpm, it is executed when Fpm's master process starts.

(2) Several main processes in this stage are as follows:

(3) Activate SAPI: sapi_activate(), initialize request information SG (request_info), set the handle for reading POST requests, etc.

(4) Start PHP output: php_output_startup()

(5) Initialize the garbage collector: gc_globals_ctor(), and allocate zend_gc_globals memory.

(6) Start the Zend engine: zend_startup(), the main operations include

(6.1) Start the memory pool start_memory_manager()

(6.2) Set some util function handles, Such as zend_error_cb, zend_pringf, zend_write, etc.

(6.3) Set Zend virtual machine compilation, executor function handles zend_compile_file, zend_execute_ex, garbage collection function handle gc_collect_cycles

(6.4) Allocate function symbol table, method symbol table (function_table), class Symbol table (class_table), constant symbol table (zend_constants), etc., if it is multi-threaded, global variables of the compiler and executor will also be allocated.

(6.5) Register Zend core extension: zend_startup_builtin_funtions(), this extension is provided by the kernel. This process will register the functions provided by Zend core extension, such as strlen, define, func_get_args, class_exists, etc.

(6.6) Register the standard constants defined by Zend: zend_register_standard_constants(), such as E_ERROR, E_WARNING, E_ALL, TRUE, FALSE, etc.

(6.7) Register the acquisition handle of the $GLOBALS super global variable.

(6.8) Allocate the storage symbol table EG (ini_directives) configured in php.ini.

(7) Register the constants defined by PHP: PHP_VERSION, PHP_ZTS, PHP_SAPI, etc.

(8) Parse php.ini: After the parsing is completed, all php.ini configurations are saved in the configuration_hash hash table.

(9) Mapping the php.ini configuration of PHP and Zend core: Obtain the corresponding configuration value based on the parsed php.ini, and insert the final configuration into the EG (ini_directives) hash table.

(10) Register handles for obtaining $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_REQUEST, $_FILES variables.

(11) Register statically compiled extensions: php_register_internal_extensions_func().

(12) Register dynamically loaded extensions: php_init_register_extensions(), load the extensions configured in php.ini into PHP.

(13) Call back the module startup hook function defined by each extension, that is, the function defined through PHP_MINIT_FUNCTION().

(14) Register disabled functions and classes in php.ini: disable_funstions, disable_classes.

3. Request initialization phase - analysis

This phase is a stage that every request will go through before request processing. For fpm, it is a stage after the worker process accepts a request and obtains and parses the request data. The processing function at this stage is php_request_startup().

The main processing is as follows:

(1) Activate output: php_output_activate()

(2) Activate Zend engine: zend_activate(), the main operations are

(2.1) Reset the garbage collector: gc_reset()

(2.2) Initialize the compiler: ini_compiler()

(2.3) Initialize the executor: ini_exexutor(), EG(function_table) and EG(class_table) execute CG(function_table) and CG(class_table) respectively.

(2.4) Initialize the global variable symbol table EG (symbol_table), the included file symbol table EG (included_files)

(2.5) Initialize the lexical scanner: sapi_activate()

(3) Activate SAPI: sapi_activate()

(4) Call back an extended definition of the request startup hook function: zend_activate_modules()

4. Script execution phase - analysis

(1) This stage includes two core stages of compilation and execution of PHP code, which is also the most important function of the Zend engine.

(2) During the compilation phase, the PHP script will go through the conversion process from PHP source code to abstract syntax tree, and then to opline instructions. The final generated opline instructions are execution instructions that can be recognized by the Zend engine. These instructions Executed by the executor, this is the process of interpretation and execution of PHP code. The entry function of this stage is php_execute_script().

5. Request closing phase - analysis

(1) This phase will flush the output content, send HTTP response headers, clean up local variables, close the compiler, and close actuator etc.

(2) In addition, the request shutdown hook function of each extension will be called back at this stage.

(3) This phase is the opposite of the request initialization phase.

6. Module shutdown phase - analysis

This phase is executed when SAPI is closed and corresponds to the module initialization phase. It mainly cleans resources and closes each PHP module. operation, and at the same time, the module shutdown hook function of each extension will be called back.

Recommended learning: php video tutorial

The above is the detailed content of What is the composition of PHP? What is the life cycle?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template