PHP Source Analysis-php life cycle _php Tutorials

Source: Internet
Author: User
Tags sapi
The two most common operating modes for PHP are Web mode, CLI mode.

Regardless of the mode, PHP works the same way, running as a SAPI.

1. When we typed the PHP command at the terminal, it was using the CLI.

It is like a Web server to support PHP to complete this request, the request is completed and then re-control to the terminal.

2, when using Apache as the host, when a request arrives, PHP will support the completion of this request.

The following macros are defined in Main/php.h

#define Php_minit_function Zend_module_startup_d

#define Php_mshutdown_function Zend_module_shutdown_d

#define Php_rinit_function Zend_module_activate_d

#define Php_rshutdown_function Zend_module_deactivate_d

#define Php_minfo_function Zend_module_info_d

#define Php_ginit_function Zend_ginit_function

#define Php_gshutdown_function Zend_gshutdown_function

The corresponding function is

Php_minit_function run when initializing module

Php_mshutdown_function run when module is unloaded

Php_rinit_function run when a request is initialized

Php_rshutdown_function run when a request is completed

Php_minfo_function This is the information for setting up this module in Phpinfo

Php_ginit_function when initializing global variables

Php_gshutdown_function when a global variable is released

Take a look at a custom extension case fragment:

Www.2cto.com

int minit_time;

Php_minit_function (Test)

{

Minit_time = time (NULL);

return SUCCESS;

}

Php_mshutdown_function (Test)

{

FILE *fp=fopen ("Mshutdown.txt", "A +");

fprintf (FP, "%ld\n", Time (NULL));//Let's see if each request ends up with data appended to this file.

Fclose (FP);

return SUCCESS;

}

int rinit_time;

Php_rinit_function (Test)

{

Rinit_time = time (NULL);

return SUCCESS;

}

Php_rshutdown_function (Test)

{

FILE *fp=fopen ("Rshutdown.txt", "A +");

fprintf (FP, "%ld\n", Time (NULL));//Let's see if each request ends up with data appended to this file.

Fclose (FP);

return SUCCESS;

}

Php_minfo_function (Test)

{

Php_info_print_table_start ();//call Php_write output HTML tag

Php_info_print_table_header (2, "module info", "Enabled");

Php_info_print_table_end ();//call Php_write output HTML tag

/* Remove Comments If you have entries in php.ini

Display_ini_entries ();

*/

}

Define the function test () that can be called in PHP, and let it output the values of Minit_time and rinit_time in the page.

Php_function (Test)

{

php_printf ("%d
", Time_of_minit);

php_printf ("%d
", Time_of_rinit);

Return

}

Take Apache, for example,

If you are working in multi-threaded mode:

In this mode, only one server process is running, but it will run many threads at the same time, which can reduce some resource overhead, just need to run it once for Module Init and module shutdown, and some global variables only need to be initialized once, because the thread has unique characteristics, Makes it possible to share some data conveniently between requests.

Multi-threaded working methods such as

If you are working in a multi-process mode:

The value of Minit_time, Rinit_time, each request is changed.
The data is written to Time_rshutdown.txt, time_mshutdown.txt each time the page request ends.

The following is a multi-process working mode diagram:


Excerpt from God's blog

http://www.bkjia.com/PHPjc/478513.html www.bkjia.com true http://www.bkjia.com/PHPjc/478513.html techarticle The two most common operating modes for PHP are Web mode, CLI mode. Regardless of the mode, PHP works the same way, running as a SAPI. 1, when we typed PHP in the terminal command ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.