let's start with the PHP architecture diagram .:
What SAPI?
SAPI's Chinese translation: Server-side application programming interface. It is responsible for PHP interaction with other applications data. Other applications here may be shell or Apache, and so on. The common SAPI are:
CGI,FAST-CGI,CLI (for the Shell), ISAPI (for IIS), Embed (for C + +), and so on.
Example: When in the shell (Command line under window) input: PHP index.php at this time, by the CLI this SAPI is responsible for the user's request processing. To define a SAPI, you need to define a struct body.
The SAPI structure of the CLI is as follows: (/PATH/TO/PHP-SRC/SAPI/CLI/PHP_CLI.C)
struct SAPI_MODULE_STRUCT defines some of the operations that are done when a CLI SAPI is started, let's take a look at the following.
1: "CLI" defines the name of the current SAPI, the Phpinfo function will use this name.
2:php_cli_startup, the complete definition of the function is
You can see that the function will call Php_module_startup. This is a PHP initialization function (see section: http://www.walu.cc/phpbook/1.3.md php life cycle)
3:php_module_shutdown_wrapper, the definition of a function is:
Just simple packing for Php_module_shutdown ()
4:php does some initialization, resource-allocation transactions for each request, where there is no corresponding handler for the CLI. But it's different for mod_php. He wants to register a resource destructor, request space, initialize environment variables, and so on in the Apache pool.
5:sapi_cli_deactivate, finishing the work.
6:sapi_cli_ub_write, provides a function handle to the Zend flush cache,
The Sapi_cli_single_write function writes data to the STDOUT
7:sapi_cli_flush,
The function calls the system's fflush to perform a cache refresh.
8: This section allows Zend to verify a script file to execute the state, judging permissions.
(not completed)
PHP Kernel: SAPI Quest