您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php扩展开发札记(2)多个源码文件的配置和编译

2024/4/24 12:09:36发布5次查看
php扩展开发笔记(2)多个源码文件的配置和编译
我们在开发过程中,为了代码得可读性和易维护性,肯定是需要多个代码文件的,而不仅仅是通过 ext_skel 生成得骨架文件。这篇文章主要介绍下,多个代码文件的时候,我们需要注意什么,以及怎么做。
我的代码文件如下(slash为我的扩展名)
// ext_skel 生成默认得模块相关的主要文件php_slash.h // 头文件slash.c // 源码文件config.m4 // 编译的时候需要修改的文件
在这个默认生成的骨架文件里面,我们只要执行下面几步,就可以完成一个新的扩展了。
删除config.m4的第10-12行开头的注释dnlphpize./configuremake && make install
通过上面的步骤,我们就完成了一个扩展,通过 dl(“slash.so”);的方式就可以进行测试了。如果编写一个类在上一篇文章里有介绍。
下面我们介绍增加新的类和源码文件以及如何编译配置等。
增加一个新的php类(slash_http_util),并且给该类增加一个 getiv 的方法,php代码测试代码如下:$httputil = new slash_http_util();$httputil->getiv(); // 会输出 hello
源码文件 slash_http_util.h#ifndef slash_http_util_h#define slash_http_util_hextern zend_class_entry *slash_http_util_ce;php_method(slash_http_util, getiv);slash_minit_function(http_util);#endif
源码文件 slash_http_util.c#ifdef have_config_h#include config.h#endif#include php.h#include php_ini.h /* for zend_alter_ini_entry */#include php_slash.h#include slash_http_util.hzend_class_entry *slash_http_util_ce;php_method(slash_http_util, getiv) { php_printf(hello); return_true;}zend_function_entry slash_http_util_methods[] = { zend_me(slash_http_util, getiv, null, zend_acc_public|zend_acc_static) php_fe_end};slash_minit_function(http_util) { zend_class_entry ce; slash_init_class_entry(ce, slash_cn_http_util, slash_http_util_methods); slash_http_util_ce = zend_register_internal_class(&ce tsrmls_cc); return success;}
以上的代码完成了新类的添加,并且给该类增加了一个叫 getiv 的方法。下面需要修改我的 config.m4 配置文件,增加 slash_http_util.c 这个文件。打开 config.m4 到最后倒数第二行,默认 ext_skel 生成的代码是
php_new_extension(slash,slash.c,$ext_shared)
修改,增加 slash_http_util.c 这个文件,需要注意的是两个 *.c 文件之间是空格,没有标点符号
php_new_extension(slash,slash.c slash_http_util.c, $ext_shared)
完成 config.m4 的修改以后,还需要去修改 slash.c 这个文件,默认情况下在这个文件中会有下面的代码
php_minit_function(slash){ /* if you have ini entries, uncomment these lines register_ini_entries(); */ return success;}
我们需要把我们新创建的这个类载入进来
php_minit_function(slash){ /* if you have ini entries, uncomment these lines register_ini_entries(); */ slash_startup(http_util); return success;}
通过这几步,新的类就完全创建成功了,你可以通过 phpize、configure、make 来编译这个模块了。最后通过我们开始制定的目标进行测试。
几个需要注意的问题:
1. 修改 config.m4 文件,增加源码文件
2. 修改 config.m4 文件以后,需要重新执行 phpize
3. 在模块加载函数中 php_minit_function 加入这个新的类。
上面的代码中如果你完全copy可能没办法通过编译,因为我自定义重命名了几个宏(php_slash.h),实际开发中你可以zend的宏。
#define slash_cn_http_util slash_http_util// {{{ php life cycle redefine#define slash_minit_function(module) php_minit_function(slash_##module)#define slash_mshutdown_function(module) php_mshutdown_function(slash_##module)#define slash_rinit_function(module) php_rinit_function(slash_##module)#define slash_rshutdown_function(module) php_rshutdown_function(slash_##module)#define slash_startup(module) php_module_startup_n(slash_##module)(init_func_args_passthru)// }}}// {{{ class init mac#define slash_init_class_entry(ce, name, methods) init_class_entry(ce, name, methods)// }}}
以上代码仅供参考,欢迎交流。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录