In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of the implementation process in PHP7. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
PHP is an interpreted language, its execution process needs to be compiled into intermediate code, and then translated into specific instructions through a specific virtual machine to be executed. The implementation process is as follows:
PHP code = > Token = > Abstract Syntax Tree = > Opcodes = > execute
The steps are as follows:
Token is obtained by lexical analysis of source code.
Token is a meaningful logo into which the PHP code is cut. PHP7-there are 137 kinds of Token, which are defined in the zend_language_parser.h file.
Transform Token into Abstract Syntax Tree (AST) based on parser
Token are lexical chunks, but individual lexical chunks can not express complete semantics, and need to be organized and concatenated with the help of certain rules. So you need the parser to match the Token according to the syntax and concatenate the Token. The product of parser concatenated with Token is abstract grammar tree (AST,Abstract Syntax Tree).
AST is a new feature of the PHP7 version, and there is no AST generated during the execution of previous versions of PHP code. Its main function is to decouple the PHP compiler and interpreter and improve the maintainability.
Convert the syntax tree to Opcode
The syntax tree needs to be converted to Opcode before it can be executed directly by the engine.
Execute Opcodes
Opcodes is the collective form of opcode and is the intermediate code during the execution of PHP. Among the optimization measures of PHP project, there is a more common "enable opcache", which refers to the technology where opcodes is cached. By omitting the phase from source code to opcode, the engine directly executes cached opacode to improve performance.
PHP7 kernel architecture
Zend engine
Lexical / syntactic analysis, AST compilation, and opcodes execution are all implemented in the Zend engine. In addition, the variable design, memory management and process management of PHP are also implemented in the engine layer.
PHP layer
The zend engine provides basic capabilities for PHP, while interactions from outside need to be handled through the PHP layer.
SAPI
Abbreviation for server API, which contains the cli SAPI and fpm SAPI of the scene. As long as the defined SAPI protocol is followed, the external module can interact with PHP.
Extension part
According to the core competence and interface specification provided by zend engine, it can be developed and extended.
PHP 7 source code structure
The main source directories of php 7 are: sapi, Zend, main, ext and TSRM.
Sapi directory
The sapi directory is an abstraction of the input and output layers and is the specification for PHP to provide external services.
Several commonly used SAPI:
1) apache2handler: Apache extension, which is compiled to generate a dynamic link library and configured under Apache. When there is a http request to Apache, the dynamic link library is called according to the configuration to execute the PHP code and complete the interaction with PHP.
2) cgi-fcgi: after compilation, an executable program that supports CGI protocol is generated. Webserver (such as NGINX) transmits the request to the CGI process through CGI protocol, and the CGI process executes the corresponding code according to the request and returns the execution result to webserver.
3) fpm-fcgi: fpm is the FastCGI process manager. Take the NGINX server as an example, when a request is sent to the NGINX server, NGINX sends the request to the php-fpm process according to the FastCGI protocol.
4) cli: command line interface of PHP
Zend directory
The Zend directory is the core code of PHP. The memory management, garbage collection, process management, variables and array implementation in PHP are all in the source code of the directory.
Main directory
The main directory is the glue between the SAPI and Zend layers. The Zend layer implements the compilation and execution of PHP scripts, the sapi layer abstracts the input and output, and the main directory acts as a connecting link between them. Above, parse the SAPI request, analyze the script files and parameters to be executed; start, and complete the necessary module initialization before calling the zend engine.
Ext directory
Ext is the directory related to the PHP extension, and a series of commonly used functions such as array, str, pdo, etc., are defined here.
TSRM
TSRM (Thread Safe Resource Manager), a thread-safe resource manager, is used to ensure the security of resource sharing.
This is the end of this article on "sample Analysis of the execution process in PHP7". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.