C++ Object Oriented PHP Extension
<p>Following the <a href="http://devzone.zend.com/article/4486">http://devzone.zend.com/article/4486</a> tutorial I've tried to wrap a couple of C++ classes to a PHP extension, unfortunately it fails. I hope there is someone who could help me. In order to try simplify the problem resolution I also simplified my classes.</p>

<p>The objective is to have classes that allow me to execute some polygonal operations. Then I've created the Point class and the Polygon class as follows:</p>

<p>polygon.h</p>

<pre><code>#ifndef POLYGON_H
#define POLYGON_H 1

#include &lt;vector&gt;

class Point {
  double __x;
  double __y;
public:
  Point(double x, double y);
  double x(void);
  double y(void);
};

class Polygon {
  std::vector&lt;Point&gt; __pts;
public:
  void add(Point pnt);
  Point&amp; get(unsigned long idx);
  unsigned long size(void);
};

#endif
</code></pre>

<p>polygon.cpp</p>

<pre><code>#include "polygon.h"

Point::Point(double x, double y) : __x(x), __y(y) {
}

double Point::x(void) {
    return __x;
}

double Point::y(void) {
    return __y;
}

void Polygon::add(Point pnt) {
    __pts.push_back(pnt);
}

Point&amp; Polygon::get(unsigned long idx) {
    return __pts.at(idx);
}

unsigned long Polygon::size(void) {
    return __pts.size();
}
</code></pre>

<p>config.m4</p>

<pre><code>PHP_ARG_ENABLE(geometry,
    [Whether to enable the "geometry" extension],
    [  --enable-geometry      Enable "geometry" extension support])

if test $PHP_GEOMETRY != "no"; then
    PHP_REQUIRE_CXX()
    PHP_SUBST(GEOMETRY_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, GEOMETRY_SHARED_LIBADD)
    PHP_NEW_EXTENSION(geometry, geometry.cpp polygon.cpp, $ext_shared)
fi
</code></pre>

<p>php_geometry.h</p>

<pre><code>#ifndef PHP_GEOMETRY_H
#define PHP_GEOMETRY_H 1

#define PHP_GEOMETRY_EXTNAME  "geometry"
#define PHP_GEOMETRY_EXTVER   "0.1"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

extern "C" {
#include "php.h"
}

extern zend_module_entry geometry_module_entry;
#define phpext_geometry_ptr &amp;geometry_module_entry;

PHP_METHOD(Point, __construct);
PHP_METHOD(Point, x);
PHP_METHOD(Point, y);

PHP_METHOD(Polygon, __construct);
PHP_METHOD(Polygon, add);
PHP_METHOD(Polygon, get);
PHP_METHOD(Polygon, size);

#endif
</code></pre>

<p>geometry.cpp</p>

<pre><code>#include "php_geometry.h"

#include "polygon.h"

zend_object_handlers point_object_handlers;
zend_object_handlers polygon_object_handlers;

struct point_object {
  zend_object std;
  Point* point;
};

struct polygon_object {
  zend_object std;
  Polygon* polygon;
};

zend_class_entry* point_ce;
zend_class_entry* polygon_ce;

void point_free_storage(void* object TSRMLS_DC) {
    point_object* obj = (point_object*)object;
    delete obj-&gt;point;

    zend_hash_destroy(obj-&gt;std.properties);
    FREE_HASHTABLE(obj-&gt;std.properties);

    efree(obj);
}

void polygon_free_storage(void* object TSRMLS_DC) {
    polygon_object* obj = (polygon_object*)object;
    delete obj-&gt;polygon;

    zend_hash_destroy(obj-&gt;std.properties);
    FREE_HASHTABLE(obj-&gt;std.properties);

    efree(obj);
}

zend_object_value point_create_handler(zend_class_entry* type TSRMLS_DC) {
    zval* tmp;
    zend_object_value retval;

    point_object* obj = (point_object*)emalloc(sizeof(point_object));
    memset(obj, 0, sizeof(point_object));
    obj-&gt;std.ce = type;

    ALLOC_HASHTABLE(obj-&gt;std.properties);
    zend_hash_init(obj-&gt;std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
    zend_hash_copy(obj-&gt;std.properties, &amp;type-&gt;default_properties, (copy_ctor_func_t)zval_add_ref, (void*)&amp;tmp, sizeof(zval*));

    retval.handle = zend_objects_store_put(obj, NULL,
        point_free_storage, NULL TSRMLS_CC);
    retval.handlers = &amp;point_object_handlers;

    return retval;
}

zend_object_value polygon_create_handler(zend_class_entry* type TSRMLS_DC) {
    zval* tmp;
    zend_object_value retval;

    polygon_object* obj = (polygon_object*)emalloc(sizeof(polygon_object));
    memset(obj, 0, sizeof(polygon_object));
    obj-&gt;std.ce = type;

    ALLOC_HASHTABLE(obj-&gt;std.properties);
    zend_hash_init(obj-&gt;std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
    zend_hash_copy(obj-&gt;std.properties, &amp;type-&gt;default_properties, (copy_ctor_func_t)zval_add_ref, (void*)&amp;tmp, sizeof(zval*));

    retval.handle = zend_objects_store_put(obj, NULL,
        polygon_free_storage, NULL TSRMLS_CC);
    retval.handlers = &amp;polygon_object_handlers;

    return retval;
}

function_entry point_methods[] = {
    PHP_ME(Point, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    PHP_ME(Point, x,           NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Point, y,           NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

function_entry polygon_methods[] = {
    PHP_ME(Polygon, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    PHP_ME(Polygon, add,         NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Polygon, get,         NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Polygon, size,        NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

PHP_MINIT_FUNCTION(geometry) {
    zend_class_entry ce;

    INIT_CLASS_ENTRY(ce, "Point", point_methods);
    point_ce = zend_register_internal_class(&amp;ce TSRMLS_CC);
    point_ce-&gt;create_object = point_create_handler;
    memcpy(&amp;point_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    point_object_handlers.clone_obj = NULL;

    INIT_CLASS_ENTRY(ce, "Polygon", polygon_methods);
    polygon_ce = zend_register_internal_class(&amp;ce TSRMLS_CC);
    polygon_ce-&gt;create_object = polygon_create_handler;
    memcpy(&amp;polygon_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    polygon_object_handlers.clone_obj = NULL;

    return SUCCESS;
}

zend_module_entry geometry_module_entry = {
#if ZEND_MODULE_API_NO &gt;= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_GEOMETRY_EXTNAME,
    NULL,        /* Functions */
    PHP_MINIT(geometry),        /* MINIT */
    NULL,        /* MSHUTDOWN */
    NULL,        /* RINIT */
    NULL,        /* RSHUTDOWN */
    NULL,        /* MINFO */
#if ZEND_MODULE_API_NO &gt;= 20010901
    PHP_GEOMETRY_EXTVER,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_GEOMETRY
extern "C" {
ZEND_GET_MODULE(geometry)
}
#endif

PHP_METHOD(Point, __construct) {
    double x = .0;
    double y = .0;
    zval *object = getThis();

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|dd", &amp;x, &amp;y) == FAILURE)
        RETURN_NULL();

    point_object *obj = (point_object *)zend_object_store_get_object(object TSRMLS_CC);
    obj-&gt;point = new Point(x, y);
}

PHP_METHOD(Point, x) {
    point_object *obj = (point_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
    Point *point = obj-&gt;point;

    if(point != NULL)
        RETURN_DOUBLE(point-&gt;x());
}

PHP_METHOD(Point, y) {
    point_object *obj = (point_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
    Point *point = obj-&gt;point;

    if(point != NULL)
        RETURN_DOUBLE(point-&gt;y());
}

PHP_METHOD(Polygon, __construct) {
    zval* object = getThis();
    polygon_object* obj = (polygon_object*)zend_object_store_get_object(object TSRMLS_CC);
    obj-&gt;polygon = new Polygon;
}

PHP_METHOD(Polygon, add) {
    polygon_object *obj = (polygon_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
    Polygon *polygon = obj-&gt;polygon;

    if(polygon != NULL) {
        zval* oth;

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &amp;oth, point_ce) == FAILURE)
            RETURN_NULL();

        point_object* ooth = (point_object*)zend_object_store_get_object(oth TSRMLS_CC);

        polygon-&gt;add(*ooth-&gt;point);
    }
}

PHP_METHOD(Polygon, get) {
    polygon_object *obj = (polygon_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
    Polygon *polygon = obj-&gt;polygon;

    if(polygon != NULL) {
        long index;

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &amp;index) == FAILURE)
            RETURN_NULL();

        if (object_init_ex(return_value, point_ce) != SUCCESS) {
        } else {
            struct point_object* vobj = (struct point_object *) zend_object_store_get_object(return_value TSRMLS_CC);
            assert (vobj != NULL);
            vobj-&gt;point = &amp;polygon-&gt;get(index);
        }
    }
}

PHP_METHOD(Polygon, size) {
    polygon_object *obj = (polygon_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
    Polygon *polygon = obj-&gt;polygon;

    if(polygon != NULL)
        RETURN_LONG(polygon-&gt;size());
}
</code></pre>

<p>Then, after compiling I try the following PHP code:</p>

<pre><code>&lt;?php
echo '&lt;pre&gt;';

$pt = new Point;

var_dump($pt);

$pl = new Polygon;
$pl-&gt;add($pt);
$pl-&gt;add(new Point(10, 0));
$pl-&gt;add(new Point(10, 10));

for($i = 0; $i &lt; $pl-&gt;size(); $i++)
    var_dump($pl-&gt;get($i));
?&gt;
</code></pre>

<p>Always crashes when executing the Polygon::get method, if I comment it, nothing wrong happens.</p>
