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

1

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

Gide answered 31/12, 2012 at 19:47 Comment(6)
Probably your best resource is the PHP5.4 source codePretonic
@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 suggestGide
php.net/manual/en/internals2.structure.php ?Gentille
@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.Gide
Maybe you could provide details about what used to work and what's stopping it from working now?Cupidity
@Cupidity - edited question with exampleGide
R
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
Rambunctious answered 1/1, 2013 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.