On the operating mechanism underlying PHP7

PHP is an interpreted language, it must first execution compiled into intermediate code, and then through the particular virtual machine, translated into a particular instruction is executed. Its implementation process is as follows:

PHP 代码 => Token => 抽象语法树 => Opcodes => 执行

The respective steps as follows:

  • Lexical source code obtained by analyzing the Token
    the Token is cut PHP code meaningful identity. PHP7 a total of 137 kinds of Token, do zend_language_parser.h defined in the file.

  • Token-based parser is converted into an abstract syntax tree (AST)
    Token word is one of a block, but can not alone block expression of the entire word semantics, but also by certain rules organized in series. So we need to match the syntax parser according to Token, the Token in series. Parser finished product series is the Token abstract syntax tree (AST, Abstract Syntax Tree).
    AST is a new feature of version PHP7, during the execution of earlier versions of PHP code is not generated AST this step. Its main function is to achieve a decoupling of PHP compiler and interpreter, improved maintainability.

  • Converting the syntax tree into Opcode
    need to convert the syntax tree into Opcode, it can be executed directly engines.

  • Performing the Opcodes
    with opcodes is the opcode of the form set, the PHP is intermediate code execution. PHP project optimization measures, there is a relatively common "open opcache", refers to techniques where the opcodes cached. By eliminating from the source to the opcode stage engine directly perform caching good opacode, to improve performance.

  • zend engine
    lexical / grammatical analysis, AST compiling and executing opcodes are implemented in the Zend engine. In addition, PHP's variable design, memory management, process management engine layer is also achieved.

  • PHP layer
    zend engine for PHP provide basic capabilities, and interaction from the outside you need to be handled by PHP layer.

  • SAPI
    abbreviation server API, which contains a scene and CLI SAPI fpm SAPI. As long as compliance with defined SAPI protocol, external modules can be completed interact with PHP.

  • Extensions
    based on core capabilities and interface specifications provided by zend engine, can develop extensions.

PHP 7 Source structure

The main source php directory 7 are: sapi, Zend, main, ext and TSRM these.

sapi directory
sapi directory is an abstract input and output layer is PHP provision of external services specifications.

Several commonly used SAPI:

1) apache2handler: Apache extension DLL to generate compiled, configured to a Apache. When a http request to Apache, the configuration will call this dynamic-link library to execute PHP code to complete the interaction with PHP.

2) cgi-fcgi: generating a compiled executable support CGI protocol, webserver (eg NGINX) protocol through the CGI request to the CGI process, CGI process will execute the appropriate code execution result is returned to the webserver upon request.

3) fpm-fcgi: fpm is FastCGI process manager. In NGINX server, for example, when a request is sent to the server NGINX, NGINX FastCGI protocol according to the request handling process php-fpm.

4) cli: PHP command-line interactive interface

    • Zend directory
      Zend PHP directory is the core code. PHP memory management, garbage collection, process management, variables, arrays, etc. to achieve in the source directory's.

    • main directory
      main directory is the SAPI layer and adhesive layer Zend. Zend layer implements PHP script compilation and execution, sapi layer implements the abstract input and output, main directory then plays a connecting role between them. Deck, SAPI request resolve, analysis parameters and script files to be executed; next start, before calling zend engine, initialization module to complete the necessary work.

    • ext directory
      ext PHP extension is relevant directories, common array, str, pdo and other functions are defined here.

    • TSRM
      TSRM (the Thread Safe Resource Manager) - thread-safe resource manager, resource sharing is used to ensure safety.

Guess you like

Origin www.cnblogs.com/gupiao777/p/11622487.html