Skip to content

Commit be427c2

Browse files
committed
add func
1 parent 9c7c45d commit be427c2

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

7/func.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## 7.6 函数
2+
3+
### 7.6.1 内部函数
4+
通过扩展可以将C语言实现的函数提供给PHP脚本使用,如同大量PHP内置函数一样,这些函数统称为内部函数(internal function),与PHP脚本中定义的用户函数不同,它们无需经历用户函数的编译过程,同时执行时也不像用户函数那样每一个指令都调用一次C语言编写的handler函数,因此,内部函数的执行效率更高。除了性能上的优势,内部函数还可以拥有更高的控制权限,可发挥的作用也更大,能够完成很多用户函数无法实现的功能。
5+
6+
#### 7.6.1.1 内部函数的定义
7+
前面介绍PHP函数的编译时曾经详细介绍过PHP函数的实现,函数通过`zend_function`来表示,这是一个联合体,用户函数使用`zend_function.op_array`,内部函数使用`zend_function.internal_function`,两者具有相同的头部用来记录函数的基本信息。不管是用户函数还是内部函数,其最终都被注册到EG(function_table)中,函数被调用时根据函数名称向这个符号表中查找。从内部函数的注册、使用过程可以看出,其定义实际非常简单,我们只需要定义一个`zend_internal_function`结构,然后注册到EG(function_table)中即可,接下来再重新看下内部函数的结构:
8+
```c
9+
typedef struct _zend_internal_function {
10+
/* Common elements */
11+
zend_uchar type;
12+
zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
13+
uint32_t fn_flags;
14+
zend_string* function_name;
15+
zend_class_entry *scope;
16+
zend_function *prototype;
17+
uint32_t num_args;
18+
uint32_t required_num_args;
19+
zend_internal_arg_info *arg_info;
20+
/* END of common elements */
21+
22+
void (*handler)(INTERNAL_FUNCTION_PARAMETERS); //函数指针,展开:void (*handler)(zend_execute_data *execute_data, zval *return_value)
23+
struct _zend_module_entry *module;
24+
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
25+
} zend_internal_function;
26+
```
27+
Common elements就是与用户函数相同的头部,用来记录函数的基本信息:函数类型、参数信息、函数名等,handler是此内部函数的具体实现,PHP提供了一个宏用于此handler的定义:`PHP_FUNCTION(function_name)``ZEND_FUNCTION()`,展开后:
28+
```c
29+
void (*handler)(zend_execute_data *execute_data, zval *return_value)
30+
```
31+
也就是内部函数会得到两个参数:execute_data、return_value,execute_data不用再说了,return_value是函数的返回值,这两个值在扩展中会经常用到。
32+
33+
比如要在扩展中定义两个函数:my_func_1()、my_func_2(),首先是编写函数:
34+
```c
35+
PHP_FUNCTION(my_func_1)
36+
{
37+
printf("Hello, I'm my_func_1\n");
38+
}
39+
40+
PHP_FUNCTION(my_func_2)
41+
{
42+
printf("Hello, I'm my_func_2\n");
43+
}
44+
```
45+
函数定义完了就需要向PHP注册了,这里并不需要扩展自己注册,PHP提供了一个内部函数注册结构:zend_function_entry,扩展只需要为每个内部函数生成这样一个结构,然后把它们保存到扩展`zend_module_entry.functions`即可,在加载扩展中会自动向EG(function_table)注册。
46+
```c
47+
typedef struct _zend_function_entry {
48+
const char *fname; //函数名称
49+
void (*handler)(INTERNAL_FUNCTION_PARAMETERS); //handler实现
50+
const struct _zend_internal_arg_info *arg_info;//参数信息
51+
uint32_t num_args; //参数数目
52+
uint32_t flags;
53+
} zend_function_entry;
54+
```
55+
zend_function_entry结构可以通过`PHP_FE()``ZEND_FE()`定义:
56+
```c
57+
const zend_function_entry mytest_functions[] = {
58+
PHP_FE(my_func_1, NULL)
59+
PHP_FE(my_func_2, NULL)
60+
PHP_FE_END //末尾必须加这个
61+
};
62+
```
63+
这几个宏的定义为:
64+
```c
65+
#define ZEND_FE(name, arg_info) ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)
66+
#define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags },
67+
#define ZEND_FN(name) zif_##name
68+
```
69+
内部函数的名称前加了`zif_`前缀,以防与内核中的函数冲突,所以如果想用gdb调试扩展定义的函数记得加上这个前缀。最后将`zend_module_entry.functions`设置为`timeout_functions`即可:
70+
```c
71+
zend_module_entry mytest_module_entry = {
72+
STANDARD_MODULE_HEADER,
73+
"mytest",
74+
mytest_functions,
75+
NULL, //PHP_MINIT(mytest),
76+
NULL, //PHP_MSHUTDOWN(mytest),
77+
NULL, //PHP_RINIT(mytest),
78+
NULL, //PHP_RSHUTDOWN(mytest),
79+
NULL, //PHP_MINFO(mytest),
80+
"1.0.0",
81+
STANDARD_MODULE_PROPERTIES
82+
};
83+
```
84+
下面来测试下这两个函数能否使用,编译安装后在PHP脚本中调用这两个函数:
85+
```php
86+
//test.php
87+
my_func_1();
88+
my_func_2();
89+
```
90+
cli模式下执行`php test.php`将输出:
91+
```
92+
Hello, I'm my_func_1
93+
Hello, I'm my_func_2
94+
```
95+
大功告成,函数已经能够正常工作了,后续的工作就是不断完善handler实现扩展自己的功能了。
96+
97+
#### 7.6.1.2 函数参数
98+
99+
#### 7.6.1.3 函数返回值
100+
101+
### 7.6.2 调用用户函数
102+
103+

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060
* [7.5 运行时配置](7/conf.md)
6161
* [7.5.1 全局变量](7/conf.md)
6262
* [7.5.2 ini配置](7/conf.md)
63+
* 7.6 函数
64+
* [7.6.1 内部函数](7/func.md)
65+
* 7.6.2 调用用户函数
6366
* 7.7 变量的操作
64-
* 7.8 函数的操作
65-
* 7.5.1 扩展中调用自定义函数
66-
* 7.5.2 创建内部函数
6767
* 7.9 面向对象
6868
* 7.6.1 扩展中创建对象
6969
* 7.6.2 创建内部类
File renamed without changes.

0 commit comments

Comments
 (0)