linux下写的php,Linux下编写一个PHP扩展

假设需求

开发一个叫做 helloWord 的扩展。

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

echo helloWord('Tom');

//返回:Hello World: Tom

本地环境

PHP版本:5.6.9

系统:Linux CentOS release 6.5 (Final)

最终效果

da764e122988c8a434533ccff57861dd.png

实现流程

第一步:

进入到本地的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

...

如图:

07140c213fd12de46bd9a9423f01425d.png

第二步:

vi php_helloWorld.h

搜索:extern zend_module_entry 新增一行:

PHP_FUNCTION(helloWorld);

如图:

33d081189b09eee8d4e224d7efe4ee6c.png

第三步:

vi helloWorld.c

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

PHP_FE(helloWorld, NULL)

如图:

4a23d921268e279e4889d0927ac4afaf.png

搜索:PHP_MINFO_FUNCTION(helloWorld)

新增版本、作者信息

php_info_print_table_row(2, "Version", "1.0");

php_info_print_table_row(2, "Author", "BiHu");

如图:

8264e9f8d2f65348de61e7c05511e231.png

在 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);

}

如图:

2e0528992eb2a0c1a9d50622da28831b.png

第四步:

//编译安装

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了。

大家可以根据自己的需求,开发满足自己的扩展。

比如,可以开发一些扩展类,扩展方法,等等。

如果大家需要helloWorld扩展包,可以关注微信公众号。

回复 “helloWorld” 即可。

一起学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值