本文通过编写一个简单的php扩展hello_world来说明php扩展是如何编写的。这个扩展没有任何的实用性,纯粹用来学习扩展如何编写的,如果真的想自己写出实用性的php扩展,还需要熟悉zend api,而且对c语言也有较高的要求。
好,进入正题。
1、进入PHP源码的ext目录下,然后执行:
./ext_skel --extname=hello_world
2、进入hello_world目录,编辑config.m4,去掉16行、18行和53行前面的dnl:
PHP_ARG_ENABLE(hello_world, whethertoenablehello_worldsupport, [ --enable-hello_world Enable hello_world support]) AC_DEFINE(HAVE_HELLO_WORLDLIB,1,[ ])
3、修改php_hello_world.h文件,把原来的
立即学习“PHP免费学习笔记(深入)”;
#define PHP_HELLO_WORLD_H改为如下内容:
#define PHP_HELLO_WORLD_H 1#define PHP_HELLO_WORLD_VERSION "1.0"#define PHP_HELLO_WORLD_EXTNAME "hello_world" PHP_FUNCTION(hello_world);//这句最关键
4、修改hello_world.c文件:
把如下内容:
const zend_function_entry hello_world_functions[] = { PHP_FE(confirm_hello_world_compiled, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in hello_world_functions[] */ };
改成:
DESTOON B2B网站管理系统是一套完善的B2B(电子商务)行业门户解决方案。系统基于PHP+MySQL开发,采用B/S架构,模板与程序分离,源码开放。模型化的开发思路,可扩展或删除任何功能;创新的缓存技术与数据库设计,可负载千万级别数据容量及访问。
const zend_function_entry hello_world_functions[] = { PHP_FE(confirm_hello_world_compiled, NULL) /* For testing, remove later. */ PHP_FE(hello_world, NULL) PHP_FE_END /* Must be the last line in hello_world_functions[] */ };
然后在尾部加入如下内容:
//扩展函数正文部分PHP_FUNCTION(hello_world){ RETURN_STRING("Hello World!",1); }
5、到ext/hello_world目录下执行如下命令
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install
6、编辑php.ini,增加如下内容:
extension=hello_world.so
测试:
在命令行执行如下内容:
php -r"echo hello_world();"
这时候会在标准输出那里看到如下内容:
HelloWorld!
至此,hello_world扩展编写完毕!
版权声明:本文为博主原创文章,转载请注明出处。
以上就介绍了PHP扩展编写入门,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。










