I need to have my PHP extension return an array of objects, but I can't seem to figure out how to do this.
我需要我的PHP扩展返回一个对象数组,但是我似乎不知道怎么做。
I have a Graph
object written in C++. Graph.getNodes()
returns a std::map<int, Node*>
. Here's the code I have currently:
我有一个用c++写的图形对象。getnodes()返回std::map
struct node_object { zend_object std; Node *node; }; zend_class_entry *node_ce;
then
然后
PHP_METHOD(Graph, getNodes) { Graph *graph; GET_GRAPH(graph, obj) // a macro I wrote to populate graph node_object* n; zval* node_zval; if (obj == NULL) { RETURN_NULL(); } if (object_init_ex(node_zval, node_ce) != SUCCESS) { RETURN_NULL(); } std::map nodes = graph->getNodes(); array_init(return_value); for (std::map::iterator i = nodes.begin(); i != nodes.end(); ++i) { php_printf("X"); n = (node_object*) zend_object_store_get_object(node_zval TSRMLS_CC); n->node = i->second; add_index_zval(return_value, i->first, node_zval); } php_printf("]"); }
When i run php -r '$g = new Graph(); $g->getNodes();'
I get the output
运行php -r '$g = new Graph()时;$g->getNodes();'我得到输出
XX]Segmentation fault
XX)段错误
meaning the getNodes() function loops successfully through my 2-node list, returns, then segfaults. What am I doing wrong?
意思是getNodes()函数通过我的2个节点列表成功地循环,返回,然后分段错误。我做错了什么?
1 个解决方案
#1
5
I simply needed to MAKE_STD_ZVAL(node_zval). A secondary issue with this code was that I was reusing this zval pointer, thus overwriting every previous zval and ending up with an array full of the same object. To remedy this, I initialize node_zval for each loop. Here's the final code:
我只需要MAKE_STD_ZVAL(node_zval)。这段代码的第二个问题是,我重用了这个zval指针,因此覆盖了之前的每个zval,最后得到了一个满是相同对象的数组。为了解决这个问题,我为每个循环初始化node_zval。这是最后的代码:
PHP_METHOD(Graph, getNodes) { Graph *graph; GET_GRAPH(graph, obj) // a macro I wrote to populate graph node_object* n; zval* node_zval; if (obj == NULL) { RETURN_NULL(); } std::map nodes = graph->getNodes(); array_init(return_value); for (std::map::iterator i = nodes.begin(); i != nodes.end(); ++i) { MAKE_STD_ZVAL(node_zval); if (object_init_ex(node_zval, node_ce) != SUCCESS) { RETURN_NULL(); } n = (node_object*) zend_object_store_get_object(node_zval TSRMLS_CC); n->node = i->second; add_index_zval(return_value, i->first, node_zval); } }
#1
5
I simply needed to MAKE_STD_ZVAL(node_zval). A secondary issue with this code was that I was reusing this zval pointer, thus overwriting every previous zval and ending up with an array full of the same object. To remedy this, I initialize node_zval for each loop. Here's the final code:
我只需要MAKE_STD_ZVAL(node_zval)。这段代码的第二个问题是,我重用了这个zval指针,因此覆盖了之前的每个zval,最后得到了一个满是相同对象的数组。为了解决这个问题,我为每个循环初始化node_zval。这是最后的代码:
PHP_METHOD(Graph, getNodes) { Graph *graph; GET_GRAPH(graph, obj) // a macro I wrote to populate graph node_object* n; zval* node_zval; if (obj == NULL) { RETURN_NULL(); } std::map nodes = graph->getNodes(); array_init(return_value); for (std::map::iterator i = nodes.begin(); i != nodes.end(); ++i) { MAKE_STD_ZVAL(node_zval); if (object_init_ex(node_zval, node_ce) != SUCCESS) { RETURN_NULL(); } n = (node_object*) zend_object_store_get_object(node_zval TSRMLS_CC); n->node = i->second; add_index_zval(return_value, i->first, node_zval); } }