


PHP extension and embedding--detailed description of PHP life cycle and variables_PHP tutorial
First, let’s introduce the life cycle of PHP. It should be very important for learning PHP and daily PHP development to understand what process a PHP program goes through from the beginning to the end.
Initiation and closing phases:
- The startup and shutdown phases of PHP can be divided into two levels,
- The first level is the initialization process of structures and values by the PHP interpreter as a whole.
- The second layer is during the request process of each page.
- For each extension, there will be an initialization MINT function. This process will declare variables, classes, register resources, streams and filter processors. These operations exist in all requests, so they can be called It is Persistent. Generally, the following two steps are performed:
- REGISTER_INI_ENTRIES()
- Initialize global variables of the module
-
After the page makes a request, PHP will establish an operating environment including the symbol table and configuration values. Then this time the PHP interpreter will cycle through each extension again and call the RINIT initialization function of each extension. The general operation of the RINT function is as follows:
- Set globalvalue to the default value. These global variables are often required for each request, but are independent of each other for each request.
- Those variables that need to be used need to be placed in the symbol table to prepare for calls.
- In this function, you can also record the relevant information of the request
-
After completing the processing of the request, if the end of the script is reached or exited through die() exit(), then PHP starts cleaning up by calling RSHUTDOWN()
- At this time, the variables in each symbol table will be unset.
- When all requests are satisfied, the MSHUTDOWN process for the module begins.
-
Calling UNREGISTER_INI_ENTRYES() corresponds to the initialization process of the MINIT function.
Life cycle of PHP program execution:
To understand the life cycle, it is necessary to have some reference to the different execution methods. PHP can be executed in several different ways, each with its own specific life cycle.
- CLI: This is to execute the php program from the command line, and its life cycle is the simplest. For example, when executing test.php, the following process is experienced
-
- Figure 1 CLI execution process of PHP
- Notice that MINIT RINIT RSHUTDOWN MSHUTDOWN is only called once, which is similar to a waterfall structure.
- Multi-threaded module method: This is the most commonly used method. As an APXS module, php cooperates with apache. When apache starts, it will fork many child processes. For multiple different requests, multiple different initialization and end processes are coordinated. But for each thread, there is only one call to MINIT and MSHUTDOWN. Each request corresponds to its own separate RINIT and RSHUTDOWN.
- Multi-threaded module method: The multi-threaded method can avoid different threads from repeatedly calling MINIT/MSHUTDOWN. It has the advantage that multiple requests can share information, but the isolation requirements between requests are relatively high, otherwise it is easy An error occurred while accessing the variable.
Zend thread safety
php has a special mechanism for thread safety, called Thread Safe Resource Management (TSRM). There are obviously some differences when making thread-safe and non-thread-safe declarations:
- Thread-safe variable declaration:
-
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 stage, you need to notify TSRM how much space this program requires through the ts_allocate_id function. TSRM will increase the current space consumption and return an id pointing to the corresponding part of the thread data pool.
- When a request is made to access data, the pointer to the resource pool of the current thread is first found from the TSRM layer, and then the resource id returned by ts_allocate_id() is added as the 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;
}- In the case of non-thread safety, you only need to simply declare variables, which is faster and more efficient. The address of the data can be determined during the compilation stage, instead of requiring runtime calculation like in the case of thread safety. At the same time, it also has the advantage that a bug in a program will not cause the entire webserver to break. Therefore, when thread safety is not needed, non-thread-safe declaration methods should be used as much as possible.
- In order to build thread-safe PHP, the --enable-maintainer-zts option must be added when compiling.When detecting in the program, you can use #ifdef ZTS. Through this detection, you can implement different processing of global variable declarations, as shown in the following example:
-
#ifdef ZTS
#define HELLO_G(v) TSRMG(hello_globals_id, zend_hello_globals *, v)
#else
#define HELLO_G(v) (hello_globals.v)
#endif- By determining whether thread safety is enabled, variables are declared and accessed in different ways.
- After thread safety is enabled, PHP will automatically enable a tsrm_ls pointer in order to distinguish data between different threads. With the help of this pointer, the functions of each thread can find the corresponding symbol table and perform corresponding variable reading operations. It is this mechanism that prevents the data space from being messed up when multiple threads work together.
By understanding the starting and ending phases, life cycle and Zend thread safety mechanism of PHP, it will be beneficial to subsequent research on PHP extended compilation.
-
Calling UNREGISTER_INI_ENTRYES() corresponds to the initialization process of the MINIT function.
Life cycle of PHP program execution:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

The combination of PHP and HTML is a common technology in web development. PHP can embed dynamic content in HTML files and implement auxiliary functions, which greatly improves the interactivity and customizability of the website. This article will introduce three techniques for embedding code and provide specific code examples for reference. 1. Use PHP tags to embed code. The most common way is to use PHP tags () to embed PHP code into HTML files to display dynamic content. For example, you can use PHP

In C++, function pointers require proper destruction and life cycle management. This can be achieved by manually destructing the function pointer and releasing the memory. Use smart pointers, such as std::unique_ptr or std::shared_ptr, to automatically manage the life cycle of function pointers. Bind the function pointer to the object, and the object life cycle manages the destruction of the function pointer. In GUI programming, using smart pointers or binding to objects ensures that callback functions are destructed at the appropriate time, avoiding memory leaks and inconsistencies.

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

How to use the Aurora Push extension to implement batch message push function in PHP applications. In the development of mobile applications, message push is a very important function. Jiguang Push is a commonly used message push service that provides rich functions and interfaces. This article will introduce how to use the Aurora Push extension to implement batch message push functionality in PHP applications. Step 1: Register a Jiguang Push account and obtain an API key. First, we need to register on the Jiguang Push official website (https://www.jiguang.cn/push)

Uniapp is a cross-platform application development framework that can build iOS, Android and Web applications at the same time. In the application development process, component life cycle hook functions are a very important part. They are used to perform corresponding operations at specific time nodes. Usually, the life cycle function of a component is automatically executed when a specific event is triggered, such as the page loading is completed, the component enters the view, the component is removed from the view, etc. However, sometimes we need to manually trigger the component's life cycle hook function in order to achieve a specific

Controlling the life cycle of a Go coroutine can be done in the following ways: Create a coroutine: Use the go keyword to start a new task. Terminate coroutines: wait for all coroutines to complete, use sync.WaitGroup. Use channel closing signals. Use context context.Context.
