使用zend编写php扩展
来源:互联网 发布:java 身份证图片识别 编辑:程序博客网 时间:2024/04/29 12:06
系统中需要的:PHP、Zend、Apache
编写的code文件:(以hello扩展模块为例) php_hello.h, hello.c, config.m4。其中,config.m4写完之后基本不用动了。其他两个文件需要不断的扩展代码。.h和普通c语言中的头文件没有什么区别,就是声明一些变量、函数等,比较特殊的是,需要声明一个与模块名称匹配的函数:
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
而.c中主要实现了我们想要的函数,并且包括了一些告知zend引擎,注册这些函数的规范式的函数(zend_module_entry hello_module_entry)、数组(static function_entry hello_functions[])。每增加一个外部函数,就修改hello_functions;而增加一些全局变量等,就需要修改hello_module_entry。
最初的三个文件如下:(摘自:http://devzone.zend.com/node/view/id/1021)
编写的code文件:(以hello扩展模块为例) php_hello.h, hello.c, config.m4。其中,config.m4写完之后基本不用动了。其他两个文件需要不断的扩展代码。.h和普通c语言中的头文件没有什么区别,就是声明一些变量、函数等,比较特殊的是,需要声明一个与模块名称匹配的函数:
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
而.c中主要实现了我们想要的函数,并且包括了一些告知zend引擎,注册这些函数的规范式的函数(zend_module_entry hello_module_entry)、数组(static function_entry hello_functions[])。每增加一个外部函数,就修改hello_functions;而增加一些全局变量等,就需要修改hello_module_entry。
最初的三个文件如下:(摘自:http://devzone.zend.com/node/view/id/1021)
config.m4
PHP_ARG_ENABLE(hello, whether to enable Hello World support,
[ --enable-hello Enable Hello World support])
if test "$PHP_HELLO" = "yes"; then
AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
php_hello.h
#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"
PHP_FUNCTION(hello_world);
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
#endif
hello.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_hello.h"
static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
{NULL, NULL, NULL}
};
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif
PHP_FUNCTION(hello_world)
{
RETURN_STRING("Hello World", 1);
}
-------------------------------------------------------------
其实config.m4不用我们自己写,上面这个架子也可以由代码自动生成,使用php源代码(注意,是源代码)中ext/ext_skel即可。
./ext_skel --extname=module_name
然后将生成的module_name目录mv到自己的目录。
执行以下步骤:
phpsize
configure --enable-module_name
make
然后将make生成的module目录下的module_name.so拷贝到php的extension_dir下即可。
最好第一步就确认以上步骤是正确的,这个可以通过运行测试代码来检查。在module_name目录下,会有module_name.php,运行:
php module_name.php 就可以了。
之后的过程就是编写你需要的代码,然后编译等了。
2008.10.09补充
RINIT是apache子进程启动时加载的,而MINIT是该模块load时加载的,两者都可以初始化全局变量,但是对性能的影响不同,前者会影响到所有进程,即使不使用该模块。
添加:这里貌似原来写错了!http://devzone.zend.com/node/view/id/1021#Heading4 根据这里的内容,当apache启动时,加载PHP引擎,然后加载所有的PHP扩展,调用它们的MINIT方法加载函数等。所以MINIT方法是全局的,影响到所有模块。而当有请求到来时,才调用对应PHP扩展的RINIT方法。所以,RINIT方法应该只影响使用了该扩展的地方。
还有一种全局变量的定义方法是按照c的方法来,比如声明全局的static int fd,然后在minit中fd=0;,在php_function中if(fd<0)fd=....;
三者具体的区别我再试试。
- 使用zend编写php扩展
- PHP扩展编写第一步:PHP和Zend介绍
- 编写PHP扩展一:PHP与Zend介绍
- 深入理解php内核 编写扩展 I:介绍PHP和Zend
- 深入理解php内核 编写扩展 I:介绍PHP和Zend
- PHP扩展1:PHP与Zend概述
- 如何编写PHP扩展
- 如何编写PHP扩展
- C++编写PHP扩展
- 如何编写PHP扩展
- 编写PHP扩展补遗
- 如何编写php扩展
- PHP扩展编写示例
- 如何编写PHP扩展
- 如何编写PHP扩展
- 如何编写PHP扩展
- PHP扩展编写点滴
- php扩展编写
- 大楼房里的小房子
- 项目的安全检查问题
- iconv 检查文件编码方式
- 做完一个小应用之后的总结
- 最近的读书计划
- 使用zend编写php扩展
- memcache -- php的memcached客户端程序源码学习
- 转:
- 使用脚本删除svn目录下的.svn文件夹
- 三年一轮回 昨天梦到笔笔了
- awk学习
- C+sqlite3
- 一切随缘 不如不见
- 几个常用的linux命令