search
Homephp教程php手册php学习笔记之PHP扩展开发教程

php学习笔记之PHP扩展开发教程

Jun 13, 2016 am 10:10 AM
phpDownnothingstudydevelopDeveloperExpandoperateTutorialofnotes

PHP扩展开发不是所有开发者都会操作的一个东西,下面我来演示一个关于PHP扩展开发实现过程,各位同学有需要可进入参考。

我们先假设需要这样一个扩展,提供一个叫tao_string的函数,他的主要作用是返回一段字符。对应的PHP代码可能是这样:

 代码如下 复制代码

function tao_string($str){
$result = $str;
return $result;
}

第一步,生成代码
PHP为了扩展开发的方便,提供了一个类似代码生成器的工具ext_skel,具体可以参见php.net官方的说明 (https://svn.php.net/repository/php/php-src/trunk/README.EXT_SKEL)。

创建一个文件tao.skel,它的内容为

string tao_string(string str)

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

 代码如下 复制代码

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

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

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

 代码如下 复制代码

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

修改为

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

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

 代码如下 复制代码

PHP_FUNCTION(tao_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, "wo shi %.78s", str);
RETURN_STRINGL(result, str_len, 0);
}

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

 代码如下 复制代码

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

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

 代码如下 复制代码

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

接下来就可以访问tao.php这个文件,测试了

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment