声明:本文为斯人原创,全部为作者一一分析得之,有不对的地方望赐教。 http://imsiren.com/archives/572 上一章用扩展创建了一个变量.. 这次来个大的..我们创建一个类. 然后在php里面去调用这个类. 生成扩展及修改 不知道的请点击这里 http://imsiren.com/a
声明:本文为斯人原创,全部为作者一一分析得之,有不对的地方望赐教。
http://imsiren.com/archives/572
上一章用扩展创建了一个变量..
这次来个大的..我们创建一个类.class Person { public $name; public $age; public function __construct() { echo "construct is running! "; } public function __destruct() { echo " destruct is running!"; } public function getproperty($key) { echo $this->$key; } public function setproperty($key,$val) { $this->$key = $val; } }
1.在php_siren.h里面声明类
PHP_METHOD(Person,__construct); PHP_METHOD(Person,__destruct); PHP_METHOD(Person,setproperty); PHP_METHOD(Person,getproperty);
PHP_METHOD宏.
PHP_METHOD 等于ZEND_METHOD
这个宏接受两个参数,第一个是类名,第二个是类的方法
#define ZEND_METHOD(classname, name) ZEND_NAMED_FUNCTION(ZEND_MN(classname##_##name)) #define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_v alue_used TSRMLS_DC //最后等于 void name(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_v alue_used TSRMLS_DC )
ZEND_BEGIN_ARG_INFO_EX(arg_person_info,0,0,2) ZEND_ARG_INFO(0,name) ZEND_END_ARG_INFO()
typedef struct _zend_arg_info { const char *name; //参数名称 zend_uint name_len;//长度 const char *class_name; //所属类名 zend_uint class_name_len; //类名长度 zend_bool array_type_hint; zend_bool allow_null; //允许为空 zend_bool pass_by_reference; //引用传值 zend_bool return_reference; //引用返回 int required_num_args; //参数个数 } zend_arg_info;
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args) \ static const zend_arg_info name[] = { \ { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },
#define ZEND_ARG_INFO(pass_by_ref, name) { #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },
static const zend_arg_info name[] = { { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args }, { #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 }, };
const zend_function_entry person_functions[]={ PHP_ME(Person,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(Person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) PHP_ME(Person,getproperty,arg_person_info,ZEND_ACC_PUBLIC) PHP_ME(Person,setproperty,arg_person_info,ZEND_ACC_PUBLIC) PHP_FE_END };
typedef struct _zend_function_entry { const char *fname; //函数名称 void (*handler)(INTERNAL_FUNCTION_PARAMETERS); const struct _zend_arg_info *arg_info;//参数 zend_uint num_args;//参数个数 zend_uint flags;//标示PUBLIC ?PRIVATE ?PROTECTED } zend_function_entry;
4.修改PHP_MINIT_FUNCTION
前面我们说过 PHP_MINIT_FUNCTION是在模块启动的时候执行的函数
首先创建一个全局指针 zend_class_entry *person_ce;
在PHP_MINIT_FUNCTION加入如下代码
zend_class_entry person; INIT_CLASS_ENTRY(person,"Person",person_functions); person_ce=zend_register_internal_class_ex(&person,NULL,NULL TSRMLS_CC); zend_declare_property_null(person_ce,ZEND_STRL("name"),ZEND_ACC_PUBLIC TSRMLS_CC);
{ \ int _len = class_name_len; \ class_container.name = zend_strndup(class_name, _len); \ class_container.name_length = _len; \ class_container.builtin_functions = functions; \ class_container.constructor = NULL; \ class_container.destructor = NULL; \ class_container.clone = NULL; \ class_container.serialize = NULL; \ class_container.unserialize = NULL; \ class_container.create_object = NULL; \ class_container.interface_gets_implemented = NULL; \ class_container.get_static_method = NULL; \ class_container.__call = handle_fcall; \ class_container.__callstatic = NULL; \ class_container.__tostring = NULL; \ class_container.__get = handle_propget; \ class_container.__set = handle_propset; \ class_container.__unset = handle_propunset; \ class_container.__isset = handle_propisset; \ class_container.serialize_func = NULL; \ class_container.unserialize_func = NULL; \ class_container.serialize = NULL; \ class_container.unserialize = NULL; \ class_container.parent = NULL; \ class_container.num_interfaces = 0; \ class_container.interfaces = NULL; \ class_container.get_iterator = NULL; \ class_container.iterator_funcs.funcs = NULL; \ class_container.module = NULL; \ }
5.创建 php_siren.h头文件中的方法体
PHP_METHOD(Person,__construct){ php_printf("construct is running<br>"); } PHP_METHOD(Person,__destruct){ php_printf("destruct is running<br>"); } PHP_METHOD(Person,setproperty){ } PHP_METHOD(Person,getproperty){ }
编译我们的扩展,
重新启动apache.
$p=new Person();
?>
我们就能在浏览器里看到输出的内容
construct is running
destruct is running
这样 ..我们用扩展创建的一个基本类就完成了.