企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 7.6.1 内部函数注册 通过扩展可以将C语言实现的函数提供给PHP脚本使用,如同大量PHP内置函数一样,这些函数统称为内部函数(internal function),与PHP脚本中定义的用户函数不同,它们无需经历用户函数的编译过程,同时执行时也不像用户函数那样每一个指令都调用一次C语言编写的handler函数,因此,内部函数的执行效率更高。除了性能上的优势,内部函数还可以拥有更高的控制权限,可发挥的作用也更大,能够完成很多用户函数无法实现的功能。 前面介绍PHP函数的编译时曾经详细介绍过PHP函数的实现,函数通过`zend_function`来表示,这是一个联合体,用户函数使用`zend_function.op_array`,内部函数使用`zend_function.internal_function`,两者具有相同的头部用来记录函数的基本信息。不管是用户函数还是内部函数,其最终都被注册到EG(function_table)中,函数被调用时根据函数名称向这个符号表中查找。从内部函数的注册、使用过程可以看出,其定义实际非常简单,我们只需要定义一个`zend_internal_function`结构,然后注册到EG(function_table)中即可,接下来再重新看下内部函数的结构: ```c typedef struct _zend_internal_function { /* Common elements */ zend_uchar type; zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */ uint32_t fn_flags; zend_string* function_name; zend_class_entry *scope; zend_function *prototype; uint32_t num_args; uint32_t required_num_args; zend_internal_arg_info *arg_info; /* END of common elements */ void (*handler)(INTERNAL_FUNCTION_PARAMETERS); //函数指针,展开:void (*handler)(zend_execute_data *execute_data, zval *return_value) struct _zend_module_entry *module; void *reserved[ZEND_MAX_RESERVED_RESOURCES]; } zend_internal_function; ``` Common elements就是与用户函数相同的头部,用来记录函数的基本信息:函数类型、参数信息、函数名等,handler是此内部函数的具体实现,PHP提供了一个宏用于此handler的定义:`PHP_FUNCTION(function_name)`或`ZEND_FUNCTION()`,展开后: ```c void *zif_function_name(zend_execute_data *execute_data, zval *return_value) { ... } ``` PHP为函数名加了"zif_"前缀,gdb调试时记得加上这个前缀;另外内部函数定义了两个参数:execute_data、return_value,execute_data不用再说了,return_value是函数的返回值,这两个值在扩展中会经常用到。 比如要在扩展中定义两个函数:my_func_1()、my_func_2(),首先是编写函数: ```c PHP_FUNCTION(my_func_1) { printf("Hello, I'm my_func_1\n"); } PHP_FUNCTION(my_func_2) { printf("Hello, I'm my_func_2\n"); } ``` 函数定义完了就需要向PHP注册了,这里并不需要扩展自己注册,PHP提供了一个内部函数注册结构:zend_function_entry,扩展只需要为每个内部函数生成这样一个结构,然后把它们保存到扩展`zend_module_entry.functions`即可,在加载扩展中会自动向EG(function_table)注册。 ```c typedef struct _zend_function_entry { const char *fname; //函数名称 void (*handler)(INTERNAL_FUNCTION_PARAMETERS); //handler实现 const struct _zend_internal_arg_info *arg_info;//参数信息 uint32_t num_args; //参数数目 uint32_t flags; } zend_function_entry; ``` zend_function_entry结构可以通过`PHP_FE()`或`ZEND_FE()`定义: ```c const zend_function_entry mytest_functions[] = { PHP_FE(my_func_1, NULL) PHP_FE(my_func_2, NULL) PHP_FE_END //末尾必须加这个 }; ``` 这几个宏的定义为: ```c #define ZEND_FE(name, arg_info) ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0) #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 }, #define ZEND_FN(name) zif_##name ``` 最后将`zend_module_entry->functions`设置为`mytest_functions`即可: ```c zend_module_entry mytest_module_entry = { STANDARD_MODULE_HEADER, "mytest", mytest_functions, //functions NULL, //PHP_MINIT(mytest), NULL, //PHP_MSHUTDOWN(mytest), NULL, //PHP_RINIT(mytest), NULL, //PHP_RSHUTDOWN(mytest), NULL, //PHP_MINFO(mytest), "1.0.0", STANDARD_MODULE_PROPERTIES }; ``` 下面来测试下这两个函数能否使用,编译安装后在PHP脚本中调用这两个函数: ```php //test.php my_func_1(); my_func_2(); ``` cli模式下执行`php test.php`将输出: ``` Hello, I'm my_func_1 Hello, I'm my_func_2 ``` 大功告成,函数已经能够正常工作了,后续的工作就是不断完善handler实现扩展自己的功能了。