Tlanyan.me /php-review-…

PHP review series directory

  • PHP based
  • Web request
  • cookie
  • Web response
  • session
  • Database operations
  • encryption
  • Composer
  • Create your own Composer package
  • Send E-mail
  • IO
  • flow
  • Socket programming
  • Multiprocess programming

This article summarizes the PHP execution flow and related concepts.

Application architecture

Let’s take a look at the support required to run a PHP program. The starting point for everything makes sense when the programmer starts writing PHP, so applying layers of PHP script files (including various third-party PHP code for Composer/include) is a must. Script files are parsed and compiled before they can be executed, so a PHP virtual machine (usually a Zend engine) is also a must. In addition, PHP scripts use multiple extension functions and classes, so extensions (including official, PECL, and user-written extensions) are almost mandatory. In addition, the SAPI is required because the PHP program interacts with the outside world (for example, fetching parameters from the command line and request information from the Web server).

To sum up, the ARCHITECTURE of PHP programs has four layers from the top down, namely: application layer, SAPI layer, extension layer and Zend engine. The architecture relationships are shown below:

(photo: www.nowamagic.net/librarys/ve)…

The SAPI layer may be relatively new to some people. SAPI provides a unified set of interfaces that decouples the upper-layer applications from the actual running environment. User-written PHP files that can be executed from the command line, Apache HTTPD or FPM. The back-up work is provided by SAPI and the developer is unaware of it. With SAPI, the PHP scripting layer doesn’t have to worry too much about the specific environment in which it is executed, while PHP itself allows SAPI to provide specific implementations for its own characteristics.

Execute the process

Regardless of the differences between SAPI implementations, the execution flow of PHP programs can be summarized as follows:

  1. The program starts, Zend engine and core components initialize;
  2. Extended initialization (MINIT);
  3. Received request, extended Activation (RINIT);
  4. Parse and execute PHP scripts;
  5. Request end, extension shutdown (RSHUTDOWN);
  6. Unloading expansion (MSHUTDOWN);
  7. The program to shut down

In addition to 345, the remaining steps are performed only once in the entire SAPI life cycle. In CGI/CLI mode, 345 is executed only once.

Understanding the life cycle of a PHP application is essential to advancing PHP and helps developers quickly locate problems. For example, if the footer function does not exist, it may be an extension missing or loading error. In CLI/CGI mode, no matter how much pconnect is used, the resource is released as soon as the script is executed. Exit /die terminates the execution of the script and does not necessarily mean the end of the process. After compiling, scripts reside in the memory and do not execute RINIT and RSHUTDOWN repeatedly, which is the performance improvement point of CLI framework compared with other running modes. And so on.

For more details on the various stages of the SAPI life cycle, see The book In Depth understanding the PHP Kernel.

CGI, FastCGI, phP-FPM, etc

CGI/FastCGI/php-cgi and php-fpm are a few concepts that can easily confuse and confuse PHP developers. The relationship between these concepts is as follows:

CGI/FastCGI: gateway protocol, language agnostic, so not very relevant to PHP. The difference is that FastCGI can be independent of the Web server, and programs running FastCGI become content providers (upstream) of the Web server. In addition, after decoupling from web server, the process interacting with FastCGI protocol has the advantages of good performance, security and stability, and support for distribution. Php-cgi: a PHP parser that implements the FastCGI protocol, without smooth restarts and hot loading; FPM: PHP official FastCGI process manager, executable program is php-fpm; Support smooth restart, hot loading, stable operation; The object it manages is not a PHP-CGI process, which is irrelevant.Copy the code

While a few concepts are easy to distinguish, what confuses developers is the combination of four concepts:

  1. Web server. Common Apache HTTPD and Nginx;
  2. SAPI. Common apache2Handler, CLI, FPM-FCgi;
  3. The agreement. CGI and FastCGI mentioned in the article;
  4. The program. Namely php-CGI and php-fPM.

Since web servers are more familiar to most people, here’s how they relate to other concepts: With Apache HTTPD, PHP scripts are executed as modules more than 90% of the time, so it has to do with apache2Handler in SAPI, not other concepts (neither CGI nor FastCGI protocols); When using Nginx, requests are forwarded to FPM 90% of the time through the FastCGI protocol, so it is related to the concepts of FPM-FCGI in SAPI, FastCGI in protocol, and PHP-FPM in program, and has nothing to do with other concepts.

conclusion

This article briefly reviews the architecture and execution flow of PHP programs, and introduces a few confusing concepts.

Thanks for reading and welcome correction!

reference

  1. www.php-internals.com/book/?p=cha…
  2. www.nowamagic.net/librarys/ve…
  3. Cuishan. Win / 2017/02/05 /…
  4. www.mike.org.cn/articles/wh…
  5. www.kancloud.cn/nickbai/php…
  6. Php.net/manual/en/i…