Start with the life cycle of PHP, understand how a PHP program starts running to the end of the process, it is important to learn PHP and PHP development in peacetime.
start and close phases:
- For PHP, the start and close stages can be divided into two tiers,
- The first layer is the initialization process for the structure and values of the PHP interpreter as a whole.
- The second layer is in the request process for each page.
- For each extension, there will be an initialization mint function, which declares variables, classes, registered resources, streams, and filter processors, which are present in all requests, so it can be called persistent. The following two steps are generally performed:
- Register_ini_entries ()
- Initialize the global variables of the module
- After the page makes a request, PHP creates an operating environment that includes the symbol table and configuration values, and then this time the PHP interpreter loops through each extension, invoking each extended rinit initialization function. The general Rint function operates as follows:
- Set Globalvalue to default values, which are often required for each request, but are independent of each request.
- The variables that need to be used are placed in the symbol table for invocation.
- In this function, you can also record information about the request.
- Once the request has been processed, if it reaches the end of the script or is rolled out through die () exit, PHP starts cleaning by calling Rshutdown ()
- The variables in each symbol table will be unset out.
- And when all the requests are fulfilled, the Mshutdown process for the module begins.
- Call Unregister_ini_entryes (), which corresponds to the initialization process of the Minit function.PHP Program execution life cycle:
To understand the life cycle, it is necessary to have a different way of doing something. PHP can be implemented in several different ways, each with its own specific life cycle.
- CLI: This is a PHP program executed from the command line, and its life cycle is the simplest. For example, when the test.php is executed, it goes through the following process.
- Figure 1 CLI execution procedure for PHP
- Note that Minit Rinit Rshutdown Mshutdown have been called only once, which is similar to the waterfall-like structure.
- Multi-threaded Module mode: This is the most common way, PHP as a APXS module for Apache to cooperate. When Apache starts, it will fork a lot of sub-processes. For many different requests, the combination is a number of different initialization and completion processes. But for each thread, there is only one minit and Mshutdown call. Each request corresponds to its own separate rinit and Rshutdown.
- Multi-threaded Module mode: Multi-threading method can avoid different thread repeated call Minit/mshutdown. It has the advantage that multiple requests can share information, but the isolation requirement between requests is high, otherwise the access error of the variable is easy to occur.
Zend Thread Safety
PHP has a special mechanism for threading security, called Thread safe Resource Management (TSRM). There are obvious differences when it comes to thread-safe and non-thread-safe declarations:
- Thread-safe variable declarations:
- typedef struct {
int sampleint;
Char *samplestring;
} php_sample_globals;
int sample_globals_id;
Php_minit_function (sample)
{
ts_allocate_id(&SAMPLE_GLOBALS_ID,
sizeof (Php_sample_globals),
(Ts_allocate_ctor) Php_sample_globals_ctor,
(ts_allocate_dtor) php_sample_globals_dtor);
return SUCCESS;
}
- As you can see from this code, in the Minit phase, the TS_ALLOCATE_ID function is required to inform TSRM how much space the program needs, TSRM increases the current space consumption, and returns an ID pointing to the appropriate portion of the thread data pool.
- When requesting access to the data, the pointer to the resource pool of the current thread is first found from the TSRM layer, followed by the resource ID returned by TS_ALLOCATE_ID () as offset.
- Non-thread-safe variable declaration:
- typedef struct {
int sampleint;
Char *samplestring;
} php_sample_globals;
Php_sample_globals sample_globals;
Php_minit_function (sample)
{
Php_sample_globals_ctor (&sample_globals tsrmls_cc);
return SUCCESS;
}