开发者

How to return array of C++ objects from a PHP extension

开发者 https://www.devze.com 2022-12-31 13:47 出处:网络
I need to have my PHP extension return an array of objects, but I can\'t seem to figure out how to do this.

I need to have my PHP extension return an array of objects, but I can't seem to figure out how to do this.

I have a Graph object written in C++. Graph.getNodes() returns a std::map<int, Node*>. Here's the code I have currently:

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);
              开发者_StackOverflow中文版  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

XX]Segmentation fault

meaning the getNodes() function loops successfully through my 2-node list, returns, then segfaults. What am I doing wrong?


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:

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);
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消