PHP_NEW_EXTENSION(php_hello, php_hello.c, $ext_shared)
前面插一个很好笑的报道:“2009/02/27日新浪:首节战罢,火箭命中率27.8%对上骑士17.6%,篮板15对7,助攻4对1”,这是我看过最搞笑的篮球比赛的命中率了:)OK~进入正题,最近忽然有兴趣研究一下PHP的内核和架构,关于PHP架构分析准备在后面的文章里推出,这篇文章主要介绍的是关于如何扩展ZendAPI的问题~
首先给大家介绍一些资料:
下面是一张介绍php系统架构的图片,可以粗略看出php模块和ZendEngine之间的关系:
然后就是实例教程了,动手吧~~
1、首先使用ext_skel建立一个PHP扩展的module骨架:
这里要注意的是ext_skel工具一般在PHP源码包的ext目录下,但是我更愿意把它提出来,也就是不在PHP源码包的ext目录下建立module,假设我现在在/home/php下建立一个module名为php_hello的module
#cd /home/php
#/path/to/ext_skel --extname=php_hello
#cd php_hello
修改config.m4文件为,简单说就是把一些dnl注释去掉即可:
PHP_ARG_WITH(php_hello, for php_hello support,
dnl Make sure that the comment is aligned:
[ --with-php_hello Include php_hello support])
或者
PHP_ARG_WITH(php_hello, for php_hello support,
dnl Make sure that the comment is aligned:
[ --with-php_hello Include php_hello support])
这样子一个扩展的module的骨架就搞定了,看看config.m4的最后:PHP_NEW_EXTENSION(php_hello, php_hello.c, $ext_shared) 这行指明了php_hello模块需要编译的目标文件,也就是php_hello.c
2、然后就是写代码,建议边看资料边写,这里我贴上自己的测试代码:
在 php_hello 目录下面新建以下文件:
php_hello.h:
#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "php_hello"
PHP_MINIT_FUNCTION(hello);
PHP_MSHUTDOWN_FUNCTION(hello);
/* defined your functions */
PHP_FUNCTION(hello_world);
PHP_FUNCTION(hello_float);
PHP_FUNCTION(hello_getobj);
PHP_FUNCTION(hello_callfunc);
extern zend_module_entry php_hello_module_entry;
#define phpext_php_hello_ptr &php_hello_module_entry
#endif
php_hello.c:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_hello.h"
/* add your functions */
static function_entry php_hello_functions[] = {
PHP_FE(hello_world, NULL)
PHP_FE(hello_float, NULL)
PHP_FE(hello_getobj, NULL)
PHP_FE(hello_callfunc, NULL)
{NULL, NULL, NULL}
};
zend_module_entry php_hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
php_hello_functions,
PHP_MINIT(hello),
PHP_MSHUTDOWN(hello),
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
PHP_INI_BEGIN()
PHP_INI_ENTRY("hello.sayto", "Default", PHP_INI_ALL, NULL)
PHP_INI_END()
#ifdef COMPILE_DL_PHP_HELLO
ZEND_GET_MODULE(php_hello)
#endif
PHP_MINIT_FUNCTION(hello)
{
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(hello)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* implement your functions */
PHP_FUNCTION(hello_world)
{
zend_printf("Hello World, %s :)", INI_STR("hello.sayto"));
}
PHP_FUNCTION(hello_float)
{
long params;
// types : l(long) | d(double) | s(string) | b(boolean) | r(resource) | a(array) | o(object) | O(specified object) | z(the actual zval*)
// notice : r | a | o | O are stored in zval*
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", ¶ms) == FAILURE) {
return;
}
RETURN_LONG(params);
}
PHP_FUNCTION(hello_getobj)
{
zval *params;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", ¶ms) == FAILURE) {
return;
}
// types : NULL 0 | LONG 1 | DOUBLE 2 | STRING 3 | ARRAY 4 | OBJECT 5 | BOOL 6 | RESOURCE 7 | CONSTANT 8 | CONSTANT_ARRAY 9
zend_printf("We get object as type %i ...", params->type);
}
PHP_FUNCTION(hello_callfunc)
{
zval **function_name;
zval *function_ret;
if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, &function_name) != SUCCESS)) {
WRONG_PARAM_COUNT;
}
if((*function_name)->type != IS_STRING) {
zend_error(E_ERROR, "Function requires string argument");
}
TSRMLS_FETCH();
if(call_user_function_ex(CG(function_table), NULL, *function_name, &function_ret, 0, NULL, 0, NULL TSRMLS_CC) != SUCCESS) {
zend_error(E_ERROR, "Function call failed");
}
zend_printf("We have %i as type ...", function_ret->type);
*return_value = *function_ret;
zval_copy_ctor(return_value);
zval_ptr_dtor(&function_ret);
}
简单分析一下源码:
php_hello.h里面主要定义了一些宏变量以及结构这里需要注意的几个方法:
ZEND_FUNCTION 这个不用说了,一看就知道是定义扩展function的函数了
详细定义请看zend_API.h的45行:
#define ZEND_NAMED_FUNCTION(name) void name(INTERNAL_FUNCTION_PARAMETERS)
#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))
#define ZEND_METHOD(classname, name) ZEND_NAMED_FUNCTION(ZEND_MN(classname##_##name))
...
至于PHP_MINIT_FUNCTION和PHP_MSHUTDOWN_FUNCTION则分别用来执行php引擎在INIT和SHUTDOWN要做的工作,它们的定义在zend_API.h的88行左右
而PHP_INI_ENTRY则是初始化ini文件的配置的函数,在程序中可以由INI_STR调用。
最后我们分别说一下几个demo方法的作用吧:
hello_world //打印,读取ini
hello_float //处理double
hello_getobj //处理object
hello_callfunc //使用回调函数
然后就是编译模块:
#/path/to/phpize
#./configure --with-php-config=/path/to/php-config
#make install
你会发现module下面出现了php_hello.so把它拷贝到/path/to/phpext目录下面,编辑/etc/php.ini,在最后加上:
[php_hello module]
extension = php_hello.so
hello.sayto = James
就可以了。
3、最后我们写一个php来测试一下吧:)
以下是测试的php的代码:
php_hello.php:
class Hello {
}
function singing() {
return 'I am singing :)';
}
echo hello_world()."/n";
echo "Float Num : ".hello_float(3.1415926)."/n";
echo hello_getobj(new Hello())."/n";
echo hello_callfunc("singing")."/n";
?>
运行看看结果吧!
Hello World, James :)
Float Num : 1293080650
We get object as type 5 ...
We have 6 as type ...I am singing :)
看到了吧,至此我们的demo成功执行了,是不是很有趣呢?下几篇我将会分析一下PHP的源码架构,实际上PHP的这套语言架构还是非常强大的,特别是Zend引擎对zval的定义,很有研究价值,慢慢深入,我们将接触到为什么PHP如此好用而流行的核心秘密了,Keep walking ...