-
-
Save krakjoe/7d64cfb4d4e7fe0d557332a9b5313698 to your computer and use it in GitHub Desktop.
a thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/sample.c b/sample.c | |
index ba8e639..c4d73db 100644 | |
--- a/sample.c | |
+++ b/sample.c | |
@@ -5,8 +5,38 @@ | |
static zend_class_entry *php_sample_greeting_class_entry; | |
+zend_object_handlers php_sample_handlers; | |
+ | |
+typedef struct _php_sample_t { | |
+ zval greeting; | |
+ zend_object std; | |
+} php_sample_t; | |
+ | |
+#define php_sample_from(o) ((php_sample_t*) ((char*) o - XtOffsetOf(php_sample_t, std))) | |
+#define php_sample_fetch(z) php_sample_from(Z_OBJ_P(z)) | |
+ | |
+zend_object* php_sample_create(zend_class_entry *ce) { | |
+ php_sample_t *s = (php_sample_t*) emalloc(sizeof(php_sample_t) + zend_object_properties_size(ce)); | |
+ | |
+ zend_object_std_init(&s->std, ce); | |
+ | |
+ object_properties_init(&s->std, ce); | |
+ | |
+ s->std.handlers = &php_sample_handlers; | |
+ | |
+ return &s->std; | |
+} | |
+ | |
+void php_sample_free(zend_object *o) { | |
+ php_sample_t *s = php_sample_from(o); | |
+ | |
+ zval_dtor(&s->greeting); | |
+ | |
+ zend_object_std_dtor(o); | |
+} | |
+ | |
PHP_METHOD(sample_Greeting, __construct) { | |
- zval *object; | |
+ php_sample_t *sample = php_sample_fetch(getThis()); | |
char *name; | |
size_t name_len; | |
@@ -17,31 +47,13 @@ PHP_METHOD(sample_Greeting, __construct) { | |
/* | |
2. Update the property with the constructor argument | |
*/ | |
- object = getThis(); | |
- zend_update_property_stringl( | |
- php_sample_greeting_class_entry, | |
- object, | |
- ZEND_STRL("name"), | |
- name, | |
- name_len | |
- ); | |
+ ZVAL_STRINGL(&sample->greeting, name, name_len); | |
} | |
PHP_METHOD(sample_Greeting, hello) { | |
- zval rv, *name, tmp; | |
- | |
- /* | |
- 3. Read the property | |
- */ | |
- name = zend_read_property(php_sample_greeting_class_entry, getThis(), ZEND_STRL("name"), 0, &rv); | |
+ php_sample_t *sample = php_sample_fetch(getThis()); | |
- /* | |
- 4. public properties can be modified from the outside, create a copy and force a string cast | |
- */ | |
- ZVAL_COPY(&tmp, name); | |
- convert_to_string(&tmp); | |
- | |
- php_printf("Hello %s!", Z_STRVAL(tmp)); | |
+ php_printf("Hello %s!", Z_STRVAL(sample->greeting)); | |
} | |
const zend_function_entry php_sample_greeting_class_functions[] = { | |
@@ -57,12 +69,11 @@ PHP_MINIT_FUNCTION(sample) | |
ce, SAMPLE_NS, PHP_SAMPLE_CLASS_GREETING_NAME, php_sample_greeting_class_functions | |
); | |
php_sample_greeting_class_entry = zend_register_internal_class(&ce TSRMLS_CC); | |
- /* | |
- 1. Declare the property | |
- */ | |
- zend_declare_property_stringl( | |
- php_sample_greeting_class_entry, ZEND_STRL("name"), ZEND_STRL("World"), ZEND_ACC_PUBLIC | |
- ); | |
+ php_sample_greeting_class_entry->create_object = php_sample_create; | |
+ | |
+ memcpy(&php_sample_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); | |
+ php_sample_handlers.offset = XtOffsetOf(php_sample_t, std); | |
+ php_sample_handlers.free_obj = php_sample_free; | |
} | |
zend_module_entry sample_module_entry = { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment