1.需要实现的细节实现一个person类 ,实现一个doing方法和saying方法
在构造方法中传递一个数组,在doing中打印此数组
saying方法中,构建一个空数组,返回,不需要传参。
2.class扩展
2.1创建类的扩展:
[root@bogon ext]# cd /usr/local/src/php-7.0.3/ext
[root@bogon ext]# ./ext_skel --extname=person
2.2 修改配置
[root@bogon ext]# vim person/config.m4dnl PHPARGWITH(person, for person support,
dnl Make sure that the comment is aligned:
dnl [ --with-person Include person support])
更改为:
PHPARGWITH(person, for person support,
dnl Make sure that the comment is aligned:
[ --with-person Include person support])
2.3 实现代码
在php_person.h头中加上extern zend_class_entry *person_ce;PHP_METHOD(person_ce,__construct);PHP_METHOD(person_ce,saying);PHP_METHOD(person_ce,doing);
在person.c头中加上ZEND_BEGIN_ARG_INFO_EX(global_config_arg, 0, 0, 1) ZEND_ARG_INFO(0, global_config)ZEND_END_ARG_INFO()/** * 声明构造函数 * @param * @return */ZEND_METHOD(person,__construct){ zval *array_config; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array_config) == FAILURE) { RETURN_NULL(); } //php_var_dump( &array_config,1 TSRMLS_CC); zend_update_property(person_ce, getThis(), "config", sizeof("config")-1, array_config TSRMLS_CC);}/** * 声明析造函数 * @param * @return */ZEND_METHOD(person,__destruct){ zend_printf("destruct\n");}ZEND_METHOD(person,doing){ //array_init(return_value); zval *array_config; zval rv; //php >=7.0 array_config = zend_read_property(person_ce, getThis(), "config", sizeof("config")-1, 0, &rv TSRMLS_DC); if( Z_TYPE_P(array_config) == IS_NULL || Z_TYPE_P(array_config) != IS_ARRAY ){ //zend_error(E_ERROR, "framework config error!"); RETURN_FALSE; } //php_var_dump(&array_config, 1 TSRMLS_CC); RETURN_ZVAL(array_config, 1, 0); //zend_printf("doing\n");}ZEND_METHOD(person,saying){ if (zend_parse_parameters_none() == FAILURE) { RETURN_FALSE; } array_init(return_value); //zend_printf("saying\n");}//这个函数需要加上声明,去掉了没用的test函数const zend_function_entry person_functions[] = { ZEND_ME(person, __construct, global_config_arg, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) ZEND_ME(person,doing,NULL,ZEND_ACC_PUBLIC) ZEND_ME(person,saying,NULL,ZEND_ACC_PUBLIC) ZEND_ME(person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) PHP_FE_END /* Must be the last line in person_functions[] */};//将类和方法注册到zendPHP_MINIT_FUNCTION(person){ zend_class_entry ce; INIT_CLASS_ENTRY(ce, "person", person_functions); person_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_declare_property_null(person_ce,"saying",strlen("saying"),ZEND_ACC_PUBLIC); zend_declare_property_null(person_ce,"doing",strlen("doing"),ZEND_ACC_PUBLIC); return SUCCESS;}
2.4 编译* [root@bogon hello]# [root@localhost person]# ./configure && make && make install
2.5 扩展安装1. 改更php.ini 加上[person] extenstion=person.so
2.6 扩展使用[root@bogon tests]# cat test.php<?php $n = new person(array('key'=>'value'));var_dump($n->saying('error'));var_dump($n->saying());var_dump($n->doing());[root@localhost tests]# php test.phpPHP Warning: person::saying() expects exactly 0 parameters, 1 given in /usr/local/src/php-7.0.3/ext/person/tests/test.php on line 5bool(false)array(0) {}array(1) { ["key"]=> string(5) "value"}
3.引用宏说明
3.1 定义参数宏 Zend/zend_API.h[root@bogon php-7.0.3]# grep -A 10 'ZEND_BEGIN_ARG_INFO_EX' ./Zend/*.h#define ZEND_BEGIN_ARG_INFO_EX(name, _unused, return_reference, required_num_args) \static const zend_internal_arg_info name[] = { \ { (const char*)(zend_uintptr_t)(required_num_args), NULL, 0, return_reference, 0, 0 },#define ZEND_BEGIN_ARG_INFO(name, _unused)\ZEND_BEGIN_ARG_INFO_EX(name, 0, ZEND_RETURN_VALUE, -1)#define ZEND_END_ARG_INFO() };//宏说明ZEND_BEGIN_ARG_INFO(name, pass_rest_by_reference) //开始参数块定义,pass_rest_by_reference为1时,强制所有参数为引用类型ZEND_END_ARG_INFO() //结束参数块定义ZEND_ARG_INFO //声明普通参数,可以用来表示PHP中的int, float, double, string等基本数据类型ZEND_ARG_OBJ_INFO //声明对象类型的参数ZEND_ARG_ARRAY_INFO //声明数组类型的参数ZEND_ARG_PASS_INFO(pass_by_ref) //pass_by_ref为1时,强制设置后续的参数为引用类型 zend_internal_arg_info结构定义在./Zend/zend_compile.h:typedef struct _zend_internal_arg_info { const char *name; //参数的名称 const char *class_name; //当参数类型为类时,指定类的名称 zend_uchar type_hint; zend_uchar pass_by_reference;//是否设置为引用,即使用&操作符 zend_bool allow_null; //是否允许设置为null zend_bool is_variadic;} zend_internal_arg_info;
3.2 定义返回值的宏 Zend/zend_API.h#define RETVAL_BOOL(b) ZVAL_BOOL(return_value, b)#define RETVAL_NULL() ZVAL_NULL(return_value)#define RETVAL_LONG(l) ZVAL_LONG(return_value, l)#define RETVAL_DOUBLE(d) ZVAL_DOUBLE(return_value, d)#define RETVAL_STR(s) ZVAL_STR(return_value, s)#define RETVAL_INTERNED_STR(s) ZVAL_INTERNED_STR(return_value, s)#define RETVAL_NEW_STR(s) ZVAL_NEW_STR(return_value, s)#define RETVAL_STR_COPY(s) ZVAL_STR_COPY(return_value, s)#define RETVAL_STRING(s) ZVAL_STRING(return_value, s)#define RETVAL_STRINGL(s, l) ZVAL_STRINGL(return_value, s, l)#define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value)#define RETVAL_RES(r) ZVAL_RES(return_value, r)#define RETVAL_ARR(r) ZVAL_ARR(return_value, r)#define RETVAL_OBJ(r) ZVAL_OBJ(return_value, r)#define RETVAL_ZVAL(zv, copy, dtor) ZVAL_ZVAL(return_value, zv, copy, dtor)#define RETVAL_FALSE ZVAL_FALSE(return_value)#define RETVAL_TRUE ZVAL_TRUE(return_value)#define RETURN_BOOL(b) { RETVAL_BOOL(b); return; }#define RETURN_NULL() { RETVAL_NULL(); return;}#define RETURN_LONG(l) { RETVAL_LONG(l); return; }#define RETURN_DOUBLE(d) { RETVAL_DOUBLE(d); return; }#define RETURN_STR(s) { RETVAL_STR(s); return; }#define RETURN_INTERNED_STR(s) { RETVAL_INTERNED_STR(s); return; }#define RETURN_NEW_STR(s) { RETVAL_NEW_STR(s); return; }#define RETURN_STR_COPY(s) { RETVAL_STR_COPY(s); return; }#define RETURN_STRING(s) { RETVAL_STRING(s); return; }#define RETURN_STRINGL(s, l) { RETVAL_STRINGL(s, l); return; }#define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; }#define RETURN_RES(r) { RETVAL_RES(r); return; }#define RETURN_ARR(r) { RETVAL_ARR(r); return; }#define RETURN_OBJ(r) { RETVAL_OBJ(r); return; }#define RETURN_ZVAL(zv, copy, dtor) { RETVAL_ZVAL(zv, copy, dtor); return; }#define RETURN_FALSE { RETVAL_FALSE; return; }#define RETURN_TRUE { RETVAL_TRUE; return; }a)返回数组array_init(return_value);//初始化return_value成数组,此操作完后就可以返回一个空的数组b)返回objectobject_init(return_value);//初始化return_value成Object,此操作完成后返回一个空的对像
3.3 类访问控制掩码 ./Zend/zend_compile.h/* method flags (types) */#define ZEND_ACC_STATIC 0x01#define ZEND_ACC_ABSTRACT 0x02#define ZEND_ACC_FINAL 0x04#define ZEND_ACC_IMPLEMENTED_ABSTRACT 0x08/* class flags (types) *//* ZEND_ACC_IMPLICIT_ABSTRACT_CLASS is used for abstract classes (since it is set by any abstract method even interfaces MAY have it s/* ZEND_ACC_EXPLICIT_ABSTRACT_CLASS denotes that a class was explicitly defined as abstract by using the keyword. */#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS 0x10#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS 0x20#define ZEND_ACC_INTERFACE 0x40#define ZEND_ACC_TRAIT 0x80#define ZEND_ACC_ANON_CLASS 0x100#define ZEND_ACC_ANON_BOUND 0x200/* method flags (visibility) *//* The order of those must be kept - public
作者:岁月经年
时间:2016年03月