我有一个 C++ 对象,Graph
它包含一个名为cat
type的属性Category
。我Graph
在用 C++ 编写的扩展中将对象暴露给 PHP。
只要Graph
's 方法返回 boolean 或 long 之类的原语,我就可以使用 ZendRETURN_*()
宏(例如RETURN_TRUE();
或RETURN_LONG(123);
。但是我怎样才能使
Graph->getCategory();
返回一个Category
供 PHP 代码操作的对象?
我正在关注http://devzone.zend.com/article/4486上的教程,这是我到目前为止的图形代码:
#include "php_getgraph.h" zend_object_handlers graph_object_handlers; 结构图对象{ zend_object 标准; 图*图; }; zend_class_entry *graph_ce; #define PHP_CLASSNAME "WFGraph" ZEND_BEGIN_ARG_INFO_EX(php_graph_one_arg, 0, 0, 1) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(php_graph_two_args, 0, 0, 2) ZEND_END_ARG_INFO() void graph_free_storage(void *object TSRMLS_DC) { 图对象*obj =(图对象*)对象; 删除 obj->graph; zend_hash_destroy(obj->std.properties); FREE_HASHTABLE(obj->std.properties); efree(obj); } zend_object_value graph_create_handler(zend_class_entry *type TSRMLS_DC) { zval *tmp; zend_object_value retval; graph_object *obj = (graph_object*)emalloc(sizeof(graph_object)); memset(obj, 0, sizeof(graph_object)); obj->std.ce = 类型; ALLOC_HASHTABLE(obj->std.properties); zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0); zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void*)&tmp, sizeof(zval*)); retval.handle = zend_objects_store_put(obj, NULL, graph_free_storage, NULL TSRMLS_CC); retval.handlers = &graph_object_handlers; 返回 retval; } PHP_METHOD(图形,__construct) { char *观点; int perspectives_len; 图 *graph = NULL; zval *object = getThis(); if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &perspectives, &perspectives_len) == FAILURE) { RETURN_NULL(); } 图 = 新图(观点); graph_object *obj = (graph_object*)zend_object_store_get_object(object TSRMLS_CC); obj->图形=图形; } PHP_METHOD(图形,有类别) { 长视角Id; 图*图; graph_object *obj = (graph_object*)zend_object_store_get_object(getThis() TSRMLS_CC); 图 = obj-> 图; 如果(图 == NULL){ RETURN_NULL(); } if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &perspectiveId) == FAILURE) { RETURN_NULL(); } RETURN_BOOL(graph->hasCategory(perspectiveId)); } PHP_METHOD(图形,getCategory) { // 在这里做什么? 返回真; } 函数入口 php_getgraph_functions[] = { PHP_ME(图形,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(Graph,hasCategory,php_graph_one_arg,ZEND_ACC_PUBLIC) PHP_ME(图,getCategory,php_graph_one_arg,ZEND_ACC_PUBLIC) {空,空,空} }; PHP_MINIT_FUNCTION(getgraph) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_CLASSNAME, php_getgraph_functions); graph_ce = zend_register_internal_class(&ce TSRMLS_CC); graph_ce->create_object = graph_create_handler; memcpy(&graph_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); graph_object_handlers.clone_obj = NULL; 返回成功; } zend_module_entry getgraph_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #万一 PHP_GETGRAPH_EXTNAME, NULL, /* 函数 */ PHP_MINIT(getgraph), NULL, /* 关机 */ 空值,/* RINIT */ NULL, /* 关机 */ NULL, /* MINFO */ #if ZEND_MODULE_API_NO >= 20010901 PHP_GETGRAPH_EXTVER, #万一 STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_GETGRAPH 外部“C”{ ZEND_GET_MODULE(getgraph) } #万一