PHP如何自定义扩展(一)之基本步骤

发布时间:2022-05-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP如何自定义扩展(一)之基本步骤脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
php自定义扩展(一

记得第一次写PHP扩展是直接百度的,照着网上写完了一个扩展,但自己不知所以然,先看看一个扩展得基本步骤吧,然后再探讨其中得原理。

利用码工具自动生成扩展目录结构

先进入php源码ext目录下执行下面命令

/www/test/php/php/bin/php ext_skel.php --ext helloworld
cd helloworld

修改config.m4配置文件,就是现在写的扩展是否用到外部依赖,就配置--wITh-hello选项,否则配置--enable-hello选项,按照自己的需求把注释去掉

dnl If your extension references something external, use 'with':
 PHP_arg_WITH([helloworld],
   [for helloworld support],
   [AS_HELP_STRING([--with-helloworld],
     [Include helloworld support])])

dnl Otherwise use 'enable':

扩展功能书写

然后vim helloworld.c 进行扩展功能代码书写
先看下模块结构定义

zend_module_entry helloworld_module_entry = {
        STANDARD_MODULE_HEADER,
        "helloworld",                                   /* Extension name */
        helloworld_functions,                   /* zend_function_entry */
        PHP_MINIT(helloworld),                                                  /* PHP_MINIT - Module initialization */
        NULL,                                                   /* PHP_MSHUTDOWN - Module shutdown */
        PHP_RINIT(helloworld),                  /* PHP_RINIT - Request initialization */
        NULL,                                                   /* PHP_RSHUTDOWN - Request shutdown */
        PHP_MINFO(helloworld),                  /* PHP_MINFO - Module info */
        PHP_HELLOWORLD_VERSION,         /* Version */
        PHP_MODULE_GLOBALS(pib),
    NULL,
    NULL,
    NULL,
    STANDARD_MODULE_PROPERTIES_EX
};

功能函数名字集合

static const zend_function_entry helloworld_functions[] = {
        PHP_FE(helloworld_test1,                arginfo_helloworld_test1)
        PHP_FE(helloworld_test2,                arginfo_helloworld_test2)
        PHP_FE_END
};

真正的功能函数代码

PHP_FUNCTION(helloworld_test2)
{
        int argc = ZEND_NUM_ARGS();
        char *messages = NULL;
        size_t   messages_len = 0;
        char *context = NULL;
        size_t   context_len = 0;

        zend_string *retval;

        ZEND_PARSE_PARAMETERS_START(0, 2)
                Z_PARAM_OPTIONAL
                Z_PARAM_STRING(messages, messages_len)
                Z_PARAM_STRING(context, context_len)
        ZEND_PARSE_PARAMETERS_END();

        retval = strpprintf(0, "Hello %s test %s", messages, context);

        RETURN_STR(retval);
}

函数参数定义

ZEND_BEGIN_ARG_INFO(arginfo_helloworld_test2, 0)
        ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

编译安装

/www/test/php/php/bin/phpize
./configure --with-php-config=/www/test/php/php/bin/php-config
make && make install

在PHP的扩展目录中已经有了helloworld.so这个文件,在php.ini中添加上扩展的配置

extension = helloworld.so

然后就可以测试自己写的函数了helloworld_test2();完成了一个扩展后,感觉自己也没什么收获,对为什么要这么写一点都不知道其中得原理,下编就来谈论其中得原理吧,先从php生命周期开始介绍,见下篇。

推荐学习:《PHP视频教程》

以上就是PHP如何自定义扩展(一)之基本步骤的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的PHP如何自定义扩展(一)之基本步骤全部内容,希望文章能够帮你解决PHP如何自定义扩展(一)之基本步骤所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:ccphp