搜尋
首頁php教程PHP开发php核心分析(一)

php核心分析(一)

Dec 19, 2016 am 11:01 AM

這裡讀的php版本為PHP-7.1.0 RC3,閱讀程式碼的平台為linux

首先是尋找php的入口,php有很多種模式,apache,php-fpm, cli模式,我要入手的話,只能先從最簡單的cli模型開始。

那麼,我需要先尋找

php -r 'echo 12;'

這個指令是如何執行的。

首先還是尋找main入口,由於我們看的是命令列的php程式。所以,這個入口在sapi/cli/php_cli.c中。


首先是定義一系列的變數

01      int c;    
02    zend_file_handle file_handle;    
03    int behavior = PHP_MODE_STANDARD;    
04    char *reflection_what = NULL;    
05    volatile int request_started = 0;    
06    volatile int exit_status = 0;    
07    char *php_optarg = NULL, *orig_optarg = NULL;    
08    int php_optind = 1, orig_optind = 1;    
09    char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;    
10    char *arg_free=NULL, **arg_excp=&arg_free;    
11    char *script_file=NULL, *translated_path = NULL;    
12    int interactive=0;    
13    int lineno = 0;    
14    const char *param_error=NULL;    
15    int hide_argv = 0;

然後是這個

sapi_module_struct *sapi_module = &cli_sapi_module;

_structsapisapisapi.它的定義在main/SAPI.h中。


下面是增加了註釋的程式碼:

01    struct _sapi_module_struct {  // SAPI模块结构    
02        char *name; // 应用层名称,比如cli,cgi等    
03        char *pretty_name; // 应用层更易读的名字,比如cli对应的就是Command Line Interface    
04    
05        int (*startup)(struct _sapi_module_struct *sapi_module); // 当一个应用要调用php的时候,这个模块启动的时候会调用的函数    
06        int (*shutdown)(struct _sapi_module_struct *sapi_module); // 当一个应用要调用php的时候,这个模块结束的时候会调用的函数    
07    
08        int (*activate)(void); // 在处理每个request的时候,激活需要调用的函数    
09        int (*deactivate)(void); // 在处理完每个request的时候,收尾时候要调用的函数    
10    
11        size_t (*ub_write)(const char *str, size_t str_length); // 这个函数告诉php如何输出数据    
12        void (*flush)(void *server_context); // 提供给php的刷新缓存的函数指针    
13        zend_stat_t *(*get_stat)(void); // 用来判断要执行文件的权限,来判断是否有执行权限    
14        char *(*getenv)(char *name, size_t name_len); // 获取环境变量的方法    
15    
16        void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); // 错误处理方法    
17    
18        int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers); // 这个函数会在我们调用header()的时候被调用    
19        int (*send_headers)(sapi_headers_struct *sapi_headers); // 发送所有的header    
20        void (*send_header)(sapi_header_struct *sapi_header, void *server_context); // 单独发送某一个header    
21    
22        size_t (*read_post)(char *buffer, size_t count_bytes); // 如何获取HTTP POST中的数据    
23        char *(*read_cookies)(void);  // 如何获取cookie中的数据    
24    
25        void (*register_server_variables)(zval *track_vars_array); // 这个函数可以给$_SERVER中获取变量    
26        void (*log_message)(char *message, int syslog_type_int); // 输出错误信息函数    
27        double (*get_request_time)(void); // 获取请求时间的函数    
28        void (*terminate_process)(void);  // TODO: 调用exit的时候调用的方法    
29    
30        char *php_ini_path_override;  // PHP的ini文件被复写了所复写的地址    
31    
32        void (*default_post_reader)(void); // 这里和前面的read_post有个差别,read_post负责如何获取POST数据,而这里的函数负责如何解析POST数据    
33        void (*treat_data)(int arg, char *str, zval *destArray); // 对数据进行处理,比如进行安全过滤等。 default_post_reader/tread_data/input_filter是三个能对输入进行过滤和处理的函数    
34        char *executable_location; // 执行的地理位置    
35    
36        int php_ini_ignore; // 是否不使用任何ini配置文件,比如php -n 就将这个位置设置为1    
37        int php_ini_ignore_cwd; // 不在当前路径寻找php.ini    
38    
39        int (*get_fd)(int *fd); // 获取执行文件的fd    
40    
41        int (*force_http_10)(void); // 强制使用http1.0    
42    
43        int (*get_target_uid)(uid_t *); // 获取执行程序的uid    
44        int (*get_target_gid)(gid_t *); // 获取执行程序的gid    
45    
46        unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len); // 对输入进行过滤。比如将输入参数填充到自动全局变量$_GET, $_POST, $_COOKIE中    
47    
48        void (*ini_defaults)(HashTable *configuration_hash); // 默认的ini配置    
49        int phpinfo_as_text; // 是否打印phpinfo信息    
50    
51        char *ini_entries; // 有没有附带的ini配置,比如使用php -d date.timezone=America/Adak,可以在命令行中设置时区    
52        const zend_function_entry *additional_functions; // 每个SAPI模块特有的一些函数注册,比如cli的cli_get_process_title    
53        unsigned int (*input_filter_init)(void); // TODO:    
54    };

有幾個點可以總結:


cli模式是不需要發送三個對忙都是空實現。


cookie也是同樣道理

1    sapi_cli_header_handler    
2    sapi_cli_send_headers    
3    sapi_cli_send_header

其他的一些定義的函數,等到我們遇到的時候再分析吧。

main

回到main函數,根據上面的那個結構,我們就理解了

sapi_cli_read_cookies

signal

1    argv = save_ps_args(argc, argv); //这里获取一次当前执行进程的参数,环境变量等。为的是对特定平台,修正下argv变量以供后续使用。    
2    
3    cli_sapi_module.additional_functions = additional_functions; // cli模式特有的函数

 以上就是php核心分析(一)的內容,更多相關內容請關注PHP中文網(www. php.cn)!


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具