9

I am trying to write an extension for php5.4 which basically wraps a very simple class in CPP.

This is for education purposes.

I find the way to do it in php5.4 has changed from php5.3

Where do I find the documentation on how to do it? Or even better, code example, any other extension that wrappes CPP classes and works in php5.4

For example, what used to work, and no longer is. Taken from http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/

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

    car_object *obj = (car_object *)emalloc(sizeof(car_object));
    memset(obj, 0, sizeof(car_object));
    obj->std.ce = type;

    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,
        car_free_storage, NULL TSRMLS_CC);
    retval.handlers = &car_object_handlers;

    return retval;
}

The line zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *)); will fail as the structure instance type (forgot it's definition) no longer has the member default_properties

6
  • 3
    Probably your best resource is the PHP5.4 source code
    – Mark Baker
    Commented Dec 31, 2012 at 20:00
  • @Mark Baker I am not great in CPP, if there is a specific extension that already does it, would be great help. Otherwise, yes, Ill do exactly what u suggest Commented Dec 31, 2012 at 20:05
  • php.net/manual/en/internals2.structure.php ?
    – Marc B
    Commented Dec 31, 2012 at 21:25
  • @Marc B there is a lot of knowledge there. Which I will read, but from a quick reading it, I see nothing about wrapping CPP classes. Commented Jan 1, 2013 at 0:18
  • Maybe you could provide details about what used to work and what's stopping it from working now?
    – Charles
    Commented Jan 1, 2013 at 2:01

1 Answer 1

6

Does the information on this PHP wiki page help?

Specifically, to address your zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *)); example, they suggest the following:

#if PHP_VERSION_ID < 50399
    zend_hash_copy(tobj->std.properties, &(class_type->default_properties),
        (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
#else
    object_properties_init(&tobj->std, class_type);
#endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.