Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a C++ extension for PHP 5.4, example code is obsolete

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

like image 932
Itay Moav -Malimovka Avatar asked Dec 31 '12 19:12

Itay Moav -Malimovka


1 Answers

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
like image 138
Ian Gregory Avatar answered Oct 02 '22 17:10

Ian Gregory