Home  >  Article  >  Backend Development  >  PHP扩展开发01:第一个扩展【转】,php01_PHP教程

PHP扩展开发01:第一个扩展【转】,php01_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:57:161018browse

PHP扩展开发01:第一个扩展【转】,php01

 

我们先假设业务场景,是需要有这么一个扩展,提供一个叫ccvita_string的函数,他的主要作用是返回一段字符。(这个业务场景实在太假,大家就这么看看吧)对应的PHP代码可能是这样:

function ccvita_string($str){
     $result = 'Link'return $result;
}

第一步,生成代码
PHP为了扩展开发的方便,提供了一个类似代码生成器的工具ext_skel,具体可以参见说明。
首先我们创建一个文件ccvita.skel,它的内容为

string ccvita_string(string str)

 

就是告诉ext_skel这个东西,我们要做的扩展里面有个函数叫ccvita_string。然后执行

cd MooENV/src/php-5.3.8/ext/
./ext_skel --extname=ccvita --proto=ccvita.skel
cd ccvita/

 

这时候,ccvita这个扩展的代码框架就已经出来了。

第二步,修改配置
然后修改config.m4文件将10、11、12三行最前面的dnl删除掉,就是将

dnl PHP_ARG_WITH(ccvita, for ccvita support,
dnl Make sure that the comment is aligned:
dnl [  --with-ccvita             Include ccvita support])

 

修改为

PHP_ARG_WITH(ccvita, for ccvita support,
Make sure that the comment is aligned:
[  --with-ccvita             Include ccvita support])

第三步,实现功能
修改源码ccvita.c文件
找到将ccvita_string这个函数修改为

PHP_FUNCTION(ccvita_string)
{
    char *str = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_len;
    char *result;
 
    if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE) 
        return;
 
    str_len = spprintf(&result, 0, "Link", str);
    RETURN_STRINGL(result, str_len, 0); 
}

 

第四步,编译扩展
保存后,开始编译

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make

第五步,添加扩展
这时候,一切顺利的话,该扩展已经在modules/ccvita.so这个位置了。下面就是将这个扩展加入到PHP中去,让我们PHP程序可以调用到。

cp modules/ccvita.so /usr/local/php/ext/
vim /usr/local/php/etc/php.ini
extension=/usr/local/php/ext/ccvita.so #在php.ini文件最后增加这一行
service php-fpm restart #重启PHP服务
cp ccvita.php /data/www/wwwroot/default/

相关代码,可以在我的github上clone出来,地址:https://github.com/KimiChen/PHP-EXT接下来就可以访问ccvita.php这个文件,测试扩展了。下一篇,将会涉及一些更深入的内容。

转自博客(http://www.ccvita.com/496.html)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1109450.htmlTechArticlePHP扩展开发01:第一个扩展【转】,php01 我们先假设业务场景,是需要有这么一个扩展,提供一个叫ccvita_string的函数,他的主要作用是返回一...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn