Go PHP execution flow, PHP extension loading process, PHP load _php tutorial

Source: Internet
Author: User
Tags sapi

Go PHP execution flow, PHP extension loading process, PHP load


Original: http://www.imsiren.com/archives/535

In order to be able to develop PHP extensions later. Be sure to understand the order in which PHP is executed. This article is to pave the way for C to develop PHP extensions.
The Web environment we assume as Apache.
When compiling PHP, in order to enable Apache to support PHP, we will generate a mod_php5.so module. Apache loads this module:
When a URL accesses a. php file, it is transferred to the Mod_php5.so module for processing. What is this thing? That's what we always say, SAPI.
The English name is: Server abstraction API.
SAPI said in fact is a collectively, under which there are isapi,cli SAPI, CGI and so on.
With it, you can easily interact with other things, such as apache,iis,cgi.
Okay, back to the chase.
Apache will register the mod_pho5.so module's hook handler when it starts. Apache is not the protagonist today, so don't dwell on it.
When Apache detects that the URL being accessed is a PHP file, control is given to SAPI.

Such as:

After entering the SAPI, the Php_init_handler function of the sapi/apache/mod_php5.c file is executed first

1234567891011121314151617181920 static void php_init_handler(server_rec *s, pool *p){ register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec); if (!apache_php_initialized) { apache_php_initialized = 1;#ifdef ZTS tsrm_startup(1, 1, 0, NULL);#endif sapi_startup(&apache_sapi_module); php_apache_startup(&apache_sapi_module); }#if MODULE_MAGIC_NUMBER >= 19980527 { TSRMLS_FETCH(); if (PG(expose_php)) { ap_add_version_component("PHP/" PHP_VERSION); } }#endif}

This function mainly calls two functions
Sapi_startup (&apache_sapi_module);
Php_apache_startup (&apache_sapi_module);

123456789101112131415 sapi_api void sapi_startup (sapi_module_struct *sf) { sf->ini_entries = NULL; sapi_module = *SF; sapi_globals_ctor (&sapi_globals); virtual_cwd_startup (); /* Could use shutdown to free the main CWD but it would just slow it down for CGI */ reentrancy_startup (); }

Sapi_startup Create a sapi_globals_struct struct.
Sapi_globals_struct holds basic information about Apache requests, such as server information, headers, encodings, and so on.

123456 staticvoidsapi_globals_ctor(sapi_globals_struct *sapi_globals TSRMLS_DC){ memset(sapi_globals, 0, sizeof(*sapi_globals)); zend_hash_init_ex(&sapi_globals->known_post_content_types, 5, NULL, NULL, 1, 0); php_setup_sapi_content_types(TSRMLS_C);}

Known_post_content_types is a hashtable that initializes its size to 5. Literally I guess it's supposed to be the content type that the client passes over. The
Php_setup_sapi_content_types function adds sapi_post_entry to the Sapi_globals
Sapi_startup execution completes Php_apache_startup

12345678 static int php_apache_startup (sapi_module_struct *sapi_module) { if (Php_module_startup (Sapi_module, &apache_module_entry, 1) = = FAILURE) { return failure; } else Code class= "CPP Plain" >{ return success; } }

Php_module_startup Too many content: is to introduce the role.
1. Initialize the zend_utility_functions structure. This structure is a function pointer that sets Zend, such as error handling functions, output functions, stream manipulation functions, and so on.
2. Set environment variables.
3. Load the php.ini configuration.
4. Load PHP built-in extensions.
5. Write the log.
6. Register the PHP internal set of functions.
7. Call Php_ini_register_extensions, load all external extensions
8. Open all Extensions
9. Some cleanup operations.
The point is, 3,4,7,8
loads the php.ini configuration
if (php_init_config (tsrmls_c) = = FAILURE) {
return FAILURE;
} The
Php_init_config function checks all the php.ini configurations here, and finds all the loaded modules and adds them to the php_extension_lists structure. The
load PHP built-in extension
calls Zend_register_standard_ini_entries to load all of the PHP's built-in extensions, such as Array,mysql.
call php_ini_register_extensions, load all external extensions
Main/php_ini.c

12345678 voidphp_ini_register_extensions(TSRMLS_D){ zend_llist_apply(&extension_lists.engine, php_load_zend_extension_cb TSRMLS_CC); zend_llist_apply(&extension_lists.functions, php_load_php_extension_cb TSRMLS_CC); zend_llist_destroy(&extension_lists.engine); zend_llist_destroy(&extension_lists.functions);}

zend_llist_apply function Traversal extension_lists execution will drop function PHP_LOAD_PHP_EXTENSION_CB
Php_load_php_extension_cb

1234 staticvoid php_load_zend_extension_cb(void *arg TSRMLS_DC){ zend_load_extension(*((char**) arg));}

Call EXT/STANDARD/DL.C zend_load_extension to load the extension,
The function skips:
The function finally calls the
if ((Module_entry = ZEND_REGISTER_MODULE_EX (module_entry tsrmls_cc)) = = NULL) {
Dl_unload (handle);
return FAILURE;
}
Put extended information into the hash table Module_registry
Zend/zend_api.c

12345 if (Zend_hash_add (&module_registry, Lcname, Name_len +1, ( void *) module, sizeof void **) &module_ptr) { ==failure zend_error (e_core_warning, Code class= "CPP Plain", module->name); efree (lcname); return null; }

At last
Zend_startup_modules (Tsrmls_c); Sort the modules and detect if they are registered to the Module_registry hash table

Zend_startup_extensions (); Execute extension->startup (extension); Start extension ...

http://www.bkjia.com/PHPjc/1125080.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125080.html techarticle []php execution flow, PHP extension loading process, PHP load Source: http://www.imsiren.com/archives/535 in order to be able to develop PHP extensions later. Be sure to understand the order in which PHP is executed. The article ...

  • Related Article

    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.