Linux下编写一个PHP扩展

假设需求javascript

开发一个叫作 helloWord 的扩展。php

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

echo helloWord('Tom');
//返回:Hello World: Tom复制代码

本地环境后端

PHP版本:5.6.9微信

系统:Linux CentOS release 6.5 (Final)函数

最终效果spa

实现流程code

第一步:cdn

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

...复制代码

如图:blog

第二步:

vi php_helloWorld.h

搜索:extern zend_module_entry 新增一行:

PHP_FUNCTION(helloWorld);复制代码

如图:

第三步:

vi helloWorld.c

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

PHP_FE(helloWorld, NULL)复制代码

如图:

搜索:PHP_MINFO_FUNCTION(helloWorld) 

新增版本、做者信息

php_info_print_table_row(2, "Version", "1.0");
php_info_print_table_row(2, "Author", "BiHu");复制代码

如图:

在 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);
}复制代码

如图:

第四步:

//编译安装
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” 便可。

Thanks ~


做者:PHP后端开发者

提供技术相关服务(本身懂的知识)。

QQ群:564557094。

关注微信公众号,留言便可,看到留言后会及时回复。

IT小圈儿
相关文章
相关标签/搜索