Learn from the director of PHP7 core (vi): Lifecycle Request initialization phase

Source: Internet
Author: User
Tags sapi zend

In the previous article we analyzed the module initialization phase of the life cycle, most of which are the initialization of global variables and the definitions of various macros, today we will learn about the second phase of the five life cycles-the request initialization phase (PHP_REQUEST_STARTUP).

I. Overview

Let's start with an overview of the functions in the request initialization phase.

function Description
Php_output_activate () Reset output global variables, initialize output related stacks
Zend_activate () Initializing the Zend Engine
Sapi_activate () Initialize SG macro, adjust each SAPI hook function activate
Zend_signal_activate () Signal Processing
Zend_set_timeout () Setting the time-out period
Php_hash_environment () Initializing global variables for PHP requests
Zend_activate_modules () Call the Request_startup hook function for each extension definition
Second, source code Analysis 2.1, Php_output_activate

Re-allocates memory for Output_globals, initializes the stack associated with the output handler, and sets the flags of the OG macro to the active state.

//main/output.cPHPAPI int php_output_activate(void){#ifdef ZTS    memset((*((void ***) ZEND_TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)], 0, sizeof(zend_output_globals));#else    memset(&output_globals, 0, sizeof(zend_output_globals));#endif    zend_stack_init(&OG(handlers), sizeof(php_output_handler *));    OG(flags) |= PHP_OUTPUT_ACTIVATED;    return SUCCESS;}
2.2, Zend_activate

Zend The initialization of the engine, the main function is to reset garbage collection, initialize the compiler, initialize the actuator, initialize the scanner.

function Description
Gc_reset () Reset Garbage Collection
Init_compiler () Initializing the compiler
Init_executor () Initialize Actuator
Startup_scanner () Initializing the scanner
2.3, Sapi_activate

Some variables within the SG macro are initialized, and the hook functions defined in the current sapi_module_struct are called activate () and Input_filter_init (), but in CLI mode, both hook functions are not implemented and NULL is returned.

//main/SAPI.cSAPI_API void sapi_activate(void){    zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);    SG(sapi_headers).send_default_content_type = 1;    /*    SG(sapi_headers).http_response_code = 200;    */    SG(sapi_headers).http_status_line = NULL;    SG(sapi_headers).mimetype = NULL;    SG(headers_sent) = 0;    ZVAL_UNDEF(&SG(callback_func));    SG(read_post_bytes) = 0;    SG(request_info).request_body = NULL;    ......}
2.4, Php_hash_environment

Allocates memory for Http_globals, initializes auto_globals, parses request parameters, and places them in global variables.

PHPAPI int php_hash_environment(void){    memset(PG(http_globals), 0, sizeof(PG(http_globals)));    zend_activate_auto_globals();    if (PG(register_argc_argv)) {        php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);    }    return SUCCESS;}
2.5, Zend_activate_modules

The function initializes by traversing all modules registered in Module_registry, invoking the hook function Request_startup () for each module.

ZEND_API void zend_activate_modules(void) /* {{{ */{    zend_module_entry **p = module_request_startup_handlers;    while (*p) {        zend_module_entry *module = *p;        if (module->request_startup_func(module->type, module->module_number)==FAILURE) {            zend_error(E_WARNING, "request_startup() for %s module failed", module->name);            exit(1);        }        p++;    }}

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.