[转]PHP的执行流程,PHP扩展加载过程,php加载
[转]PHP的执行流程,PHP扩展加载过程,php加载
原文:http://www.imsiren.com/archives/535
为了以后能开发PHP扩展..就一定要了解PHP的执行顺序..这篇文章就是为C开发PHP扩展做铺垫.
web环境 我们假设为 apache.
在编译PHP的时候,为了能够让Apache支持PHP,我们会生成一个mod_php5.so的模块.apache加载这个模块..
在url访问.php文件的时候就会转给mod_php5.so模块来处理.这个玩意是什么..就是我们常说的SAPI
英文名字是:Server abstraction API.
SAPI说的其实是一个统称,其下有 ISAPI,CLI SAPI, CGI等.
有了它,就可以很容易的跟其他东西交互.比如APACHE,IIS,CGI等.
好了回到正题.
apache启动后会将mod_pho5.so模块的hook handler注册进来.apache今天不是主角,所以不细说.
当APACHE检测到 访问的url是一个php文件时,这时候就会把控制权交给sapi.
如下图:
进入到sapi后,首先会执行sapi/apache/mod_php5.c 文件的php_init_handler函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
}
|
该函数主要调用 两个函数
sapi_startup(&apache_sapi_module);
php_apache_startup(&apache_sapi_module);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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创建一个 sapi_globals_struct结构体.
sapi_globals_struct保存了apache请求的基本信息,如服务器信息,header,编码等.
1 2 3 4 5 6 |
static void sapi_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是一个HashTable,将其大小初始化为5.从字面意义上我猜测它保存的应该是客户端传递过来的内容类型.
php_setup_sapi_content_types函数将sapi_post_entry添加到sapi_globals里
sapi_startup执行完毕后再执行php_apache_startup
1 2 3 4 5 6 7 8 |
static int php_apache_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
return FAILURE;
} else {
return SUCCESS;
}
}
|
php_module_startup 内容太多..是介绍一下作用.
1. 初始化zend_utility_functions 结构.这个结构是设置zend的函数指针,比如错误处理函数,输出函数,流操作函数等.
2. 设置环境变量.
3. 加载php.ini配置.
4. 加载php内置扩展.
5. 写日志.
6. 注册php内部函数集.
7. 调用 php_ini_register_extensions,加载所有外部扩展
8. 开启所有扩展
9. 一些清理操作.
重点说一下 3,4,7,8
加载php.ini配置
if (php_init_config(TSRMLS_C) == FAILURE) {
return FAILURE;
}
php_init_config函数会在这里检查所有php.ini配置,并且找到所有加载的模块,添加到php_extension_lists结构中.
加载php内置扩展
调用 zend_register_standard_ini_entries加载所有php的内置扩展,如array,mysql等.
调用 php_ini_register_extensions,加载所有外部扩展
main/php_ini.c
1 2 3 4 5 6 7 8 |
void php_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函数遍历extension_lists 执行会掉函数 php_load_php_extension_cb
php_load_php_extension_cb
1 2 3 4 |
static void php_load_zend_extension_cb( void *arg TSRMLS_DC)
{
zend_load_extension(*(( char **) arg));
}
|
调用 ext/standard/dl.c zend_load_extension 加载扩展,
该函数略过..
该函数最后调用
if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
DL_UNLOAD(handle);
return FAILURE;
}
将 扩展信息 放到 Hash表module_registry中
Zend/zend_API.c
1 2 3 4 5 |
if (zend_hash_add(&module_registry, lcname, name_len+1, ( void *)module, sizeof (zend_module_entry), ( void **)&module_ptr)==FAILURE) {
zend_error(E_CORE_WARNING, "Module '%s' already loaded" , module->name);
efree(lcname);
return NULL;
}
|
最后
zend_startup_modules(TSRMLS_C); //对模块进行排序,并检测是否注册到module_registry HASH表里
zend_startup_extensions(); //执行extension->startup(extension);启动扩展…

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

AI Hentai Generator
Generate AI Hentai for free.

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



Introduction: In this article, this website will introduce to you the relevant content about the Mac keyboard not responding when entering passwords. I hope it will be helpful to you, let’s take a look. How to solve the problem that the password cannot be entered on the Apple Mac system? You can also try to connect this keyboard to other computers to see if it works normally. If the same problem occurs on another computer, it's most likely a fault with the keyboard itself. You may consider replacing the keyboard or repairing it. The first time you log in to Alipay with your Mac computer, you will find the login interface and cannot enter your password. The password input field displays: "Please click here to install the control", so Alipay login requires the installation of security controls. Security controls can encrypt and protect the information you enter (amount, password, etc.) to improve account security. Tie

Many Mac users tend to keep the default name of their device and may never consider changing it. Many people choose to stick with the name from the initial setup, such as "Johnny's MacBook Air" or simply "iMac." Learning how to change the name of your Mac is a very useful skill, especially when you have multiple devices, as it can help you quickly distinguish and manage them. Next, we will teach you step by step how to change the computer name, host name and Bonjour name (local host name) in macOS system. Why should you change your Mac name? Changing the name of your Mac can not only show your personality, but also help improve the user experience: Personalize your Mac: The default name may not be to your taste, change it to a name you like.

Introduction: In this article, this website will introduce to you the relevant content about forgetting the password of the Mac installation program. I hope it will be helpful to you, let’s take a look. What to do if you forget your password for Apple computer installation software. First, find iCloud in the phone settings and click to open it. Next, enter your account number and password. There is a line of small words below the login button that prompts you if you have forgotten your ID or password. Click this option. Normally, after you enter an incorrect password multiple times on the login interface, your MacBook Pro will prompt you to use your bound Apple ID to reset your password. You only need to follow the steps prompted by the system to complete the password reset. 3. First shut down your Mac, then restart it. While pressing the power button, immediately press and hold com on the keyboard.

Preface: Today, this site will share with you the relevant content about installing pkg files on Mac. If it can solve the problem you are facing now, don’t forget to follow this site and start now! The previous version of macos pkg cannot be installed to upgrade the operating system: If your laptop is using an older operating system version, it is recommended to upgrade to the latest operating system version. Because older versions may not support installation of the latest macOS system. Select "Erase" in Disk Utility, then select the Macos extension in the format, do not check the encryption option, and do not select the apfs format, and finally click the "Erase" button to solve the problem of being unable to complete the macOS installation. Drag the application's icon to the file starting with App

Introduction: Today, this site will share with you relevant content about how to turn pages when typing on Apple Mac. If it can solve the problem you are facing now, don’t forget to follow this site and start now! Tips for using the touchpad on Apple MacBook laptops. The steps for setting up two-finger sliding on Apple computers are as follows: Find the "Settings" icon on the computer desktop and click it. Select "Touchpad" in the settings interface, and then click "Scroll to Zoom". Check "Scroll direction: Natural" in the scroll zoom options to complete the setting. Setting up a two-finger swipe method on your Apple computer is easy. First, turn on your computer and click on the Settings icon at the top of the screen. In the settings interface, select the "Touchpad" option. Then click "Scroll Zoom" and make sure "Scroll Direction" is checked

Recently, some friends have consulted the editor about how to set up WeChat Mac to automatically convert voice messages into text. The following is a method for setting up WeChat Mac to automatically convert voice messages into text. Friends in need can come and learn more. Step 1: First, open the Mac version of WeChat. As shown in the picture: Step 2: Next, click "Settings". As shown in the picture: Step 3: Then, click "General". As shown in the picture: Step 4: Then check the option "Automatically convert voice messages in chat to text". As shown in the picture: Step 5: Finally, close the window. As shown in the picture:

Introduction: This article is here to introduce you to the relevant content of cutting files to the hard disk on Mac. I hope it will be helpful to you, let’s take a look. How to Export Photos to a Mobile Hard Drive on a Mac Computer You can use the Finder or Photos application to export photos to a mobile hard drive. When using Finder to export photos, first make sure the mobile hard drive is connected to the Mac and is successfully recognized by the Mac. The steps to transfer photos from Mac to hard drive are simple: first, connect the mobile hard drive to the computer, and then open the [Launcher] icon. Then, find and click the [Photos] icon in the pop-up window. In the opened [Photos] window, hold down the [Shift] key

By default, iPhone takes photos from the camera in HEIC format. HEIC stands for High Efficiency Image Container and can hold more pixel data than PNG or JPG files, taking up significantly less space on iPhone storage compared to other formats. These files work best on iPhones but are not widely accepted on the internet because they often result in blurry/grainy pictures when you share them with non-Apple devices. To ensure that HEIC images are compatible on other devices, you may need to convert them to JPG format. This article will introduce how to convert HEIC images to JPG on Mac. How to Convert HEIC Photos to JPG on Mac [3 Methods] Method
