目录
- String 的两个宏定义
- 类的初始化 INIT_CLASS_ENTRY
- 类实体的注册 zend_register_internal_class
- 成员函数参数 ZEND_BEGIN_ARG_INFO_EX
- 声明类的成员变量 zend_declare_property
- 声明类常量 zend_declare_class_constant
- 类的初始化示例
- 获取对象句柄 Z_OBJ_HANDLE
- 修改类对象/静态成员变量的值 zend_update_property
- 获取对象自身this 指针 getThis
- 在扩展内创建对象 object_init
- 在扩展中调用对象的方法 zend_call_method_with_0_params
- 获取对象成员变量值 zend_read_property
String 的两个宏定义
在Zend 内核中,针对字符串,定义了两个宏, 这个在后续的字符串处理中经常需要用到,大家留意
----zend_portability.h----
#define ZEND_STRL(str) (str), (sizeof(str)-1)
#define ZEND_STRS(str) (str), (sizeof(str))
类的初始化
//初始化类实体
void INIT_CLASS_ENTRY(zend_class_entry ce, char *classname, zend_function_entry *functions);
参数 |
用途 |
ce |
类实体 zend_class_entry ,存储类的信息,通常用该指针代表一个类 |
classname |
类名称 |
functions |
类的成员方法, 定义在 zend_function_entry 结构体数组中。 |
类实体的注册
//注册一般类实体
zend_class_entry *zend_register_internal_class(zend_class_entry *ce TSRMLS_DC);
//注册带父类的实体
zend_class_entry *zend_register_internal_class_ex(zend_class_entry *ce,
zend_class_entry *parent_ce, char *parent_name TSRMLS_DC);
//注册内部类
zend_class_entry *zend_register_internal_interface(zend_class_entry *ce TSRMLS_DC);
参数 |
用途 |
ce |
之前被INIT_CLASS_ENTRY初始化过的类实体 |
parent_ce |
已经注册过的该类的父类实体 |
parent_name |
父类的类名 |
一个空类,连构造函数也没有的类实体的注册示例:
----php_swoole.h----
extern zend_class_entry *swoole_server_class_entry_ptr; //类实体的声明
----swoole.c----
//类成员函数的实体
static zend_function_entry swoole_server_methods[] = {
{NULL, NULL, NULL}
};
//类实体
zend_class_entry swoole_server_ce;
zend_class_entry *swoole_server_class_entry_ptr;
...
PHP_MINIT_FUNCTION(swoole)
{
//类的初始化
INIT_CLASS_ENTRY(swoole_server_ce, "swoole_server", swoole_server_methods);
swoole_server_class_entry_ptr = zend_register_internal_class(&swoole_server_ce TSRMLS_CC);
...
}
成员函数参数
//类成员函数的参数宏定义
ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args)
ZEND_ARG_PASS_INFO(by_ref)
ZEND_ARG_INFO(by_ref, name)
ZEND_ARG_ARRAY_INFO(by_ref, name, allow_null)
ZEND_ARG_OBJ_INFO(by_ref, name, classname, allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX 宏用于声明参数的属性
参数 |
用途 |
name |
参数信息名 |
pass_rest_by_reference |
是否引用传参,如果这个参数设置为1, 所有没有在结构中显式描述的参数都被认为是编译期引用传值的参数. |
return_reference |
引用返回,告诉Zend你的函数需要用自己的zval覆写return_value_ptr. |
required_num_args |
必输的参数个数 |
其他的宏用于描述,参数的具体类型
宏 |
用途 |
ZEND_ARG_PASS_INFO |
标识是否对应的参数应该被强制为引用传值 |
ZEND_ARG_INFO |
普通参数, name 为参数名 |
ZEND_ARG_ARRAY_INFO |
传入参数必须为数组, name 为参数名, allow_null 是否可以为空 |
ZEND_ARG_OBJ_INFO |
传入参数必须为对象,name 为参数名, classname 为对象名, allow_null 是否可空 |
声明类的成员变量
int zend_declare_property(zend_class_entry *ce, char *name, int name_length,
zval *value, int access_type TSRMLS_DC);
int zend_declare_property_ex(zend_class_entry *ce, char *name, int name_length,
zval *value, int access_type, char *doc_comment, int doc_comment_len TSRMLS_DC);
int zend_declare_property_null(zend_