Home Backend Development PHP Tutorial [转]PHP的执行流程,PHP扩展加载过程

[转]PHP的执行流程,PHP扩展加载过程

Jun 23, 2016 pm 01:07 PM

为了以后能开发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);启动扩展…

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles