int le_conn; //资源表示定义成全局,在模块初始化阶段,zend会给每个资源分配唯一的id
zend_class_entry *spread_class_entry; //全局定义类
static zend_function_entry spread_class_functions[] = { //类方法可以直接映射到函数,这样既可以直接访问函数也可以面向对象,mysqli应该就是这种方式
ZEND_MALIAS(Spread, __construct, spread_constructor, arginfo_spread___construct, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(connect, spread_connect, arginfo_spread_oop_connect, 0)
PHP_ME_MAPPING(disconnect, spread_disconnect, arginfo_spread_oop_disconnect, 0)
PHP_ME_MAPPING(join, spread_join, arginfo_spread_oop_join, 0)
PHP_ME_MAPPING(leave, spread_leave, arginfo_spread_oop_join, 0)
PHP_ME_MAPPING(receive, spread_receive, arginfo_spread_oop_receive, 0)
PHP_ME_MAPPING(multicast, spread_multicast, arginfo_spread_oop_multicast, 0)
PHP_FE_END
};
//定义宏为类添加常量
#define REGISTER_SPREAD_CLASS_CONST_LONG(const_name, value) \
zend_declare_class_constant_long(spread_class_entry, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
PHP_MINIT_FUNCTION(spread) //模块初始化中定义类
{
zend_class_entry spread_ce;
memcpy(&spread_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
spread_object_handlers.clone_obj = NULL;
INIT_CLASS_ENTRY(spread_ce, "Spread", spread_class_functions);
spread_ce.create_object = spread_object_new;
spread_class_entry = zend_register_internal_class(&spread_ce TSRMLS_CC);
//定义资源
le_conn = zend_register_list_destructors_ex(php_spread_rsr_dtor, NULL, "spread", module_number);
}
ZEND_REGISTER_RESOURCE(return_value, mbox, le_conn); //注册资源,通过le_conn关联定义的资源,mbox为资源变量,资源析构函数php_spread_rsr_dtor参数zend_rsrc_list_entry *rsrc的ptr指针指向mbox。
//更新类属性
zend_update_property_stringl(spread_class_entry, this, "daemon", sizeof("daemon") - 1, spread_name, spread_name_len TSRMLS_CC);
//直接定义类方法
PHP_METHOD(Spread, spread_constructor)
{
zval *object = getThis(); //获取当前类对象
zval *groups_array;
//添加属性
add_property_bool(object, "connected", 0);
add_property_null(object, "daemon");
add_property_null(object, "private_name");
add_property_null(object, "private_group");
MAKE_STD_ZVAL(groups_array);
array_init(groups_array);
add_property_zval(object, "joined_groups", groups_array);
/* add_prop_zval increments the refcount, useless here */
zval_ptr_dtor(&groups_array);
}
retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) spread_object_free_storage, NULL TSRMLS_CC); //将资源放入资源池,返回索引
(ze_spread_object *) zend_object_store_get_object(object TSRMLS_CC); //获取对象池中bucket中的对象,传入值为对象