【视频】从零开始编写第一个PHP扩展
Rango会讲解在Linux下从零开始写一个PHP扩展,并编译安装到PHP里,一直到执行扩展中的函数。包含的内容有:
- 为什么要开发PHP扩展
- ext_skel工具的使用
- 修改config.m4
- phpize的使用
- 必备工具,autoconf / gcc / make / php5 / php5-dev
- ZendAPI的使用简介,会介绍几个重要的API
摘自:http://wiki.swoole.com/wiki/page/238.html
学习笔记:
1、安装 Eclipse IDE 插件
(1) 去官网下载 Eclipse 3.7
http://www.eclipse.org/downloads/packages/release/indigo/sr2
(2) 安装 IDE插件
Help -> Install New Software
Work width -> 选择 “Indigo - http://download.eclipse.org/releases/indigo”
Programming Languages ->
选择“C/C++ Development Tools”
选择“PHP Development Tools(PDT) SDK Feature”
(3) 下载PHP源码
2、安装开发工具(3个)
sudo apt-get install gcc make autoconf
- gcc:C语言的编译器
- make:C/C++工程自动构建工具
- autoconf:检测一些系统环境,以及一些编译参数的设置
3、开发扩展
(1) 重要工具 ./ext/ext_skel
为我们自动创建一个PHP扩展工程的工具/脚本,执行它一下,会打印一些帮助信息
$ ./ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
[--skel=dir] [--full-xml] [--no-help] --extname=module module is the name of your extension
--proto=file file contains prototypes of functions to create
--stubs=file generate only function stubs in file
--xml generate xml documentation to be added to phpdoc-cvs
--skel=dir path to the skeleton directory
--full-xml generate xml documentation for a self-contained extension
(not yet implemented)
--no-help don't try to be nice and create comments in the code
and helper functions to test if the module compiled
(2) 利用 ext_skel 创建工程
$ ./ext_skel --extname=test
Creating directory test
Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [done]. To use your new extension, you will have to execute the following steps: 1. $ cd ..
2. $ vi ext/test/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-test
5. $ make
6. $ ./sapi/cli/php -f ext/test/test.php
7. $ vi ext/test/test.c
8. $ make Repeat steps 3-6 until you are satisfied with ext/test/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
(3) 进入到 扩展 test 目录中
config.m4:autoconf 工具的配置文件
php_test.h:头文件
test.c:C源文件
开发扩展的第一步,就是修改 config.m4,把我们的扩展启用,dnl 是 autoconf 的 注释符,把
dnl PHP_ARG_WITH(test, for test support,
dnl Make sure that the comment is aligned:
dnl [ --with-test Include test support])
改为
PHP_ARG_WITH(test, for test support,
[ --with-test Include test support])
表示 启用了这个扩展
(4) 在 扩展test 目录下,执行命令
$ /opt/software/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
PHP根据刚才修改的 config.m4 文件,生成一个 configure 脚本,执行成功后,会看到这个目录就多了一些文件。
我们最主要看的是 configure 这个文件,它是一个Shell脚本,这个脚本帮我们检测一些头文件,一些环境特性方面的东西。
(5) 执行 configure 文件
sudo ./configure --with-php-config=/opt/software/php/bin/php-config
执行成功后,在扩展目录下会产生一个 Makefile 文件,它是 make 命令的配置文件,
(6) make 根据 Makefile 文件,对 C 源文件进行编译。
$ sudo make
会在扩展的 modules 目录下,生成 test.so 文件
(7) 安装扩展
将刚才编译好的扩展(test.so)安装到PHP的扩增目录
$ sudo make install
Installing shared extensions: /opt/software/php/lib/php/extensions/no-debug-non-zts-20100525/
(8) 启用扩展
通过如下命令,找到 php.ini 文件位置,
$ /opt/software/php/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /opt/software/php/etc
Loaded Configuration File => /opt/software/php/etc/php.ini
编译 php.ini,添加
extension=test.so
(9) 查看扩展是否被启用
$ /opt/software/php/bin/php -m
[PHP Modules]
bcmath
bz2
...
sysvshm
test
tokenizer
...
zip
zlib [Zend Modules]
Xdebug
(10) 自己重新写一个函数 test_hello
php_test.h 文件中,增加
PHP_FUNCTION(test_hello);
test.c 文件中,增加
PHP_FUNCTION(test_hello)
{
long a;
long b;
char *c;
int c_len;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &a, &b, &c, &c_len) == FAILURE) {
return;
}char *str;
int len = spprintf(&str, 0, "%s: %d\n", c, a * b);RETURN_STRINGL(str, len, 0);
}
再重新编译、安装 sudo make && make install
(11) 检测 自定义函数 test_hello 是否存在
$ php --rf 'test_hello'
Exception: Function test_hello() does not exist
提示 函数不存在,说明该函数还没有注册到 Zend引擎中
(12) 注册 test_hello函数到 Zend引擎中
在 test.c 文件中找到如下代码块:
/* {{{ test_functions[]
*
* Every user visible function must have an entry in test_functions[].
*/
const zend_function_entry test_functions[] = {
PHP_FE(confirm_test_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in test_functions[] */
};
增加 test_hello 函数的注册
const zend_function_entry test_functions[] = {
PHP_FE(test_hello, NULL)
PHP_FE(confirm_test_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in test_functions[] */
};
(13) 重新 编译、安装
$ sudo make && make install
(14) 重新 检测函数是否存在
$ php --rf 'test_hello'
Function [ <internal:test> function test_hello ] {
}
OK,函数注册Zend成功!
【视频】从零开始编写第一个PHP扩展的更多相关文章
- 从零开始编写一个BitTorrent下载器
从零开始编写一个BitTorrent下载器 BT协议 简介 BT协议Bit Torrent(BT)是一种通信协议,又是一种应用程序,广泛用于对等网络通信(P2P).曾经风靡一时,由于它引起了巨大的流量 ...
- 从零开始创建一个 PHP 扩展
创建一个扩展的基本步骤都有哪些.示例中,我们将实现如下功能: <?phpecho say();?> 输出内容: $ php ./test.php$ hello word 在扩展中实现一个s ...
- 从零开始编写一个vue插件
title: 从零开始编写一个vue插件 toc: true date: 2018-12-17 10:54:29 categories: Web tags: vue mathjax 写毕设的时候需要一 ...
- 从零开始编写自己的C#框架(11)——创建解决方案
这段时间一直在充电,拜读了园子中大神们的博文(wayfarer的<设计之道>.TerryLee的<.NET设计模式系列文章>.卡奴达摩的<设计模式>还有其他一些零散 ...
- 从零开始编写自己的C#框架(2)——开发前准备工作
没想到写了个前言就受到很多朋友的支持,大家的推荐就是我最大的动力(推荐得我热血沸腾,大家就用推荐来猛砸我吧O^-^O),谢谢大家支持. 其实框架开发大家都知道,不过要想写得通俗点,我个人觉得还是挺吃力 ...
- 从零开始编写自己的C#框架(8)——后台管理系统功能设计
还是老规矩先吐下槽,在规范的开发过程中,这个时候应该是编写总体设计(概要设计)的时候,不过对于中小型项目来说,过于规范的遵守软件工程,编写太多文档也会拉长进度,一般会将它与详细设计合并到一起来处理,所 ...
- Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序
原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows- ...
- Laravel 项目中编写第一个 Vue 组件
和 CSS 框架一样,Laravel 不强制你使用什么 JavaScript 客户端框架,但是开箱对 Vue.js 提供了良好的支持,如果你更熟悉 React 的话,也可以将默认的脚手架代码替换成 R ...
- [转帖] 从零开始编写自己的C#框架(27)——什么是开发框架
从零开始编写自己的C#框架(27)——什么是开发框架 http://www.cnblogs.com/EmptyFS/p/4105713.html 没写过代码 一直不清楚 框架的含义 不过看了一遍 也没 ...
随机推荐
- STM32的bulk双缓冲传输速度的讨论,硬件的坑永远填不完
详情:http://bbs.21ic.com/forum.php?mod=viewthread&tid=109584 USB 1.0的最高12Mbps. USB 2.0的高速模式480Mb ...
- vs2015发现一个字符串拼接 bug
VS2015支持 字符串拼接 如下: string user="test"; int password=123; string sql=$" user={user};pa ...
- this,this,再次讨论javascript中的this,超全面
至于js中this这个东西,好多淫解释过了,看起来好高端的样子,不造你看懂了木有? 先引用比较高端的,“汤姆大叔“ 的,yes this 好了,下面加上鄙人比较挫的解释 论点: this 不是变量,不 ...
- MFC实现Gif动画制作工具
每天来博客园逛,看里面各种好文章,发现自己已经许久没有分享点什么了. 前几天用MFC设计了一个小型的Gif动画制作工具,思路如下: 1.支持图片格式:"*.jpg","* ...
- php检测php.ini是否配制正确
运行命令行 php -d display_startup_errors=1 -d error_reporting=-1 -d display_errors -c "C:\path-to-ph ...
- css圆角矩形及去掉空格属性
css圆角矩形 -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; 去掉空格 white-space:nowra ...
- 微信支付PHP SDK —— 公众号支付代码详解
在微信支付 开发者文档页面 下载最新的 php SDK http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 这里假设你已经申请完微 ...
- 关于软工项目beta版本
项目总结 项目成员: 黄丰润 031302307 王旭銮 031302320 张家俊 031302329 张晓燕 031302343 项目完成度:实现了专业信息填写.查看,教师信息填写,报课和查看课表 ...
- GIthub的小技巧
目录: 一.快捷键一览表 二.快速搜索项目文件功能 三.使用Github Pages搭建项目网站 一.快捷键一览表 具体操作: 在各个页面下按下shift+/也就是?都可以打开键盘快捷 ...
- (练习)rational rose进行用例图设计
用例图: