Skip to content

Instantly share code, notes, and snippets.

@flavius
Created August 31, 2011 13:14
Show Gist options
  • Select an option

  • Save flavius/1183512 to your computer and use it in GitHub Desktop.

Select an option

Save flavius/1183512 to your computer and use it in GitHub Desktop.
#include <zend.h>
#include "php_demo.h"
PHP_METHOD(Bar, __construct) {
long three;
zval *four;
php_printf("%s called\n", __FUNCTION__);
if(FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
&three)) {
WRONG_PARAM_COUNT;
}
zend_update_property_long(demo_bar_ce, getThis(), "three", sizeof("three")-1, three TSRMLS_CC);
MAKE_STD_ZVAL(four);
array_init(four);
zend_update_property(demo_bar_ce, getThis(), "four", sizeof("four")-1, four TSRMLS_CC);
}
static const function_entry demo_bar_functions[] = {
PHP_ME(Bar, __construct, NULL, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
ZEND_RAW_FENTRY(NULL, NULL, NULL, 0)
};
PHP_METHOD(Foo, __construct) {
php_printf("%s called\n", __FUNCTION__);
}
static const function_entry demo_foo_functions[] = {
PHP_ME(Foo, __construct, NULL, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
ZEND_RAW_FENTRY(NULL, NULL, NULL, 0)
};
zend_class_entry *demo_foo_ce;
zend_class_entry *demo_bar_ce;
zend_object_handlers demo_bar_handlers;
void demo_destroy_bar(void *object, zend_object_handle handle TSRMLS_DC) {
zend_object *obj;
php_printf("%s called\n", __FUNCTION__);
obj = object;
zend_objects_destroy_object(object, handle TSRMLS_CC);
}
void demo_free_bar(void *object TSRMLS_DC) {
zend_object *obj;
zval **four;
obj = object;
php_printf("%s called\n", __FUNCTION__);
if(SUCCESS == zend_hash_find(obj->properties, "four", sizeof("four"), (void**)&four)) {
if(Z_REFCOUNT_PP(four) > 1) {
zval_ptr_dtor(four);
}
}
zend_objects_free_object_storage(object TSRMLS_CC);
}
zend_object_value demo_create_bar(zend_class_entry *ce TSRMLS_DC) {
zend_object_value retval;
zend_object *obj;
obj = emalloc(sizeof(zend_object));
zend_object_std_init(obj, ce TSRMLS_CC);
zend_hash_copy(obj->properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval*));
retval.handle = zend_objects_store_put(obj, demo_destroy_bar, demo_free_bar, NULL TSRMLS_CC);
retval.handlers = &demo_bar_handlers;
return retval;
}
PHP_MINIT_FUNCTION(demo) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Foo", demo_foo_functions);
demo_foo_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_long(demo_foo_ce, "one", sizeof("one")-1, 1, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(demo_foo_ce, "two", sizeof("two")-1, 2, ZEND_ACC_PUBLIC TSRMLS_CC);
INIT_CLASS_ENTRY(ce, "Bar", demo_bar_functions);
demo_bar_ce = zend_register_internal_class_ex(&ce, demo_foo_ce, "Foo" TSRMLS_CC);
zend_declare_property_long(demo_bar_ce, "three", sizeof("three")-1, 3, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(demo_bar_ce, "four", sizeof("four")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
memcpy(&demo_bar_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
demo_bar_ce->create_object = demo_create_bar;
return SUCCESS;
}
zend_module_entry demo_module_entry = {
STANDARD_MODULE_HEADER,
PHP_DEMO_EXTNAME,
NULL,
PHP_MINIT(demo),
NULL,
NULL,
NULL,
NULL,
PHP_DEMO_EXTVER,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_DEMO
ZEND_GET_MODULE(demo)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment