【视频】从零开始编写第一个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 没写过代码 一直不清楚 框架的含义 不过看了一遍 也没 ...
随机推荐
- html 空格-有趣的试验
首先,先给大家看一组demo <input /> <input type="submit" /> 展示效果: 为什么会出现空格呢?input不是行内元素吗? ...
- C#出题库项目的总结(2)
前记:好吧好吧,我好好的自我检讨,这个总结拖了这么久才来写,而且一周多没有看技术相关的东西,实在罪过,不过因为想做的事情太多,所以时间的分配确实是一个很严肃的问题,不是时间不够用,是我自己没有做好时间 ...
- 删除 windows 下 node_modules 过深的目录
本文同步自我的个人博客:http://www.52cik.com/2015/11/13/node-modules-del.html 说到 node 的模块,确实既好用又蛋疼.相信无数人吐槽 node_ ...
- [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)
本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...
- 云计算之路-阿里云上:Wireshark抓包分析一个耗时20秒的请求
这篇博文分享的是我们针对一个耗时20秒的请求,用Wireshark进行抓包分析的过程. 请求的流程是这样的:客户端浏览器 -> SLB(负载均衡) -> ECS(云服务器) -> S ...
- VS类自定义版权注释
对IDE快捷方式右键-属性-打开文件位置,找到..\Microsoft Visual Studio 10.0\Common7\IDE文件夹下的..\ItemTemplates\CSharp\Code\ ...
- [codevs1283]等差子序列(二进制)
题目:http://codevs.cn/problem/1283/ 分析: 主要就是在每个判定上节省时间.一般的做法是开个数组记录每个数字出没出现,然后每次读入一个数字就以他为中间向两边扩展直到两个对 ...
- [BZOJ2038]小Z的袜子(莫队算法)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 分析:莫队算法 莫队算法是一种思想…… 处理问题:不带修改的区间询问 使用要求:[l-1 ...
- 第九课:js的类与继承
因为本书是js的框架设计,因此观看本书的必须有js基础,看不懂,请不要觉得自己差.我也是看了5遍js高级程序设计,才能看懂这本书的. 有关js的构造函数,继承的方法大家可以去看js的高级程序设计,我这 ...
- 每天一个linux命令(53):wget命令
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...