怎么在Linux下编写一个PHP扩展

发布时间:2021-09-13 17:38:51 作者:chen
来源:亿速云 阅读:125

这篇文章主要讲解了“怎么在Linux下编写一个PHP扩展”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么在Linux下编写一个PHP扩展”吧!

假设需求

开发一个叫做 helloWord 的扩展。

扩展里有一个函数,helloWord()。

echo helloWord('Tom');
//返回:Hello World: Tom

本地环境

PHP版本:5.6.9

系统:Linux CentOS release 6.5 (Final)

最终效果

怎么在Linux下编写一个PHP扩展

实现流程

第一步:

进入到本地的php目录执行:

cd /root/soft/src/php-5.6.9
cd ext
./ext_skel --extname=helloWord
cd helloWord
vi config.m4

搜索:dnl Otherwise use enable 将下面修改成:

PHP_ARG_ENABLE(helloWorld, whether to enable helloWorld support,
[  --enable-helloWorld           Enable helloWorld support])

if test "$PHP_HELLOWORLD" != "no"; then

...

如图:

怎么在Linux下编写一个PHP扩展

第二步:

vi php_helloWorld.h

搜索:extern zend_module_entry 新增一行:

PHP_FUNCTION(helloWorld);

如图:

怎么在Linux下编写一个PHP扩展

第三步:

vi helloWorld.c

搜索:const zend_function_entry helloWorld_functions[] 新增一行:

PHP_FE(helloWorld, NULL)

如图:

怎么在Linux下编写一个PHP扩展

在 helloWorld.c 底部新增一个方法

PHP_FUNCTION(helloWorld)
{
    char *arg = NULL;
    int arg_len, len;
    char *strg;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        return;
    }
    len = spprintf(&strg, 0, "Hello World: %s", arg);
    RETURN_STRINGL(strg, len, 0);
}

如图:

怎么在Linux下编写一个PHP扩展

第四步:

//编译安装
cd /root/soft/src/php-5.6.9/ext
/usr/local/php/bin/phpize #用phpize生成configure配置文件
./configure --with-php-config=/usr/local/php/bin/php-config   #配置
make  #编译
make install  #安装

第五步:

//修改php.ini
extension="helloWorld.so"   #名称为安装扩展的名称

第六步:

重启环境。

完成上面的步骤,简单的 helloWorld 扩展就OK了。

感谢各位的阅读,以上就是“怎么在Linux下编写一个PHP扩展”的内容了,经过本文的学习后,相信大家对怎么在Linux下编写一个PHP扩展这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. linux下安装php扩展memcache
  2. linux下配置php扩展mssql

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux php

上一篇:怎么创建未定义类PHP对象

下一篇:如何使用php foreach修改值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》