|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 5 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1997-2014 The PHP Group | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.01 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.php.net/license/3_01.txt | |
| 11 | + | If you did not receive a copy of the PHP license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | [email protected] so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Author: | |
| 16 | + +----------------------------------------------------------------------+ |
| 17 | +*/ |
| 18 | + |
| 19 | +/* $Id$ */ |
| 20 | + |
| 21 | +#ifdef HAVE_CONFIG_H |
| 22 | +#include "config.h" |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "php.h" |
| 26 | +#include "php_ini.h" |
| 27 | +#include "ext/standard/info.h" |
| 28 | +#include "php_objects3.h" |
| 29 | + |
| 30 | + |
| 31 | +static zend_class_entry *hello_ce; |
| 32 | +static zend_object_handlers hello_handlers; |
| 33 | + |
| 34 | + |
| 35 | +typedef struct _hello_object { |
| 36 | + zend_object obj; |
| 37 | + |
| 38 | + char *name; |
| 39 | + int name_len; |
| 40 | + long age; |
| 41 | +} hello_object; |
| 42 | + |
| 43 | +#define HELLO_FETCH_OBJECT(zobj) (hello_object *)zend_object_store_get_object((zobj) TSRMLS_CC) |
| 44 | + |
| 45 | +ZEND_BEGIN_ARG_INFO(hello___construct_arginfo, 0) |
| 46 | + ZEND_ARG_INFO(0, name) |
| 47 | + ZEND_ARG_INFO(0, age) |
| 48 | +ZEND_END_ARG_INFO() |
| 49 | +static PHP_METHOD(Hello, __construct) { |
| 50 | + hello_object *objval = HELLO_FETCH_OBJECT(getThis()); |
| 51 | + char *name; |
| 52 | + int name_len; |
| 53 | + long age; |
| 54 | + |
| 55 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &age) == FAILURE) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + objval->name = estrndup(name, name_len); |
| 60 | + objval->name_len = name_len; |
| 61 | + objval->age = age; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +static PHP_METHOD(Hello, greet) { |
| 66 | + hello_object *objval = HELLO_FETCH_OBJECT(getThis()); |
| 67 | + |
| 68 | + php_printf("Hello, "); |
| 69 | + PHPWRITE(objval->name, objval->name_len); |
| 70 | + php_printf(", you appear to be %ld years old\n", objval->age); |
| 71 | +} |
| 72 | + |
| 73 | +static zend_function_entry hello_methods[] = { |
| 74 | + PHP_ME(Hello, __construct, hello___construct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 75 | + PHP_ME(Hello, greet, NULL, ZEND_ACC_PUBLIC) |
| 76 | + {NULL, NULL, NULL} |
| 77 | +}; |
| 78 | + |
| 79 | +static void hello_dtor(hello_object *objval TSRMLS_CC) { |
| 80 | + if (objval->name) { |
| 81 | + efree(objval->name); |
| 82 | + } |
| 83 | + zend_object_std_dtor(&(objval->obj) TSRMLS_CC); |
| 84 | + |
| 85 | + efree(objval); |
| 86 | +} |
| 87 | + |
| 88 | +static zend_object_value hello_ctor(zend_class_entry *ce TSRMLS_DC) { |
| 89 | + hello_object *objval = emalloc(sizeof(hello_object)); |
| 90 | + memset(objval, 0, sizeof(hello_object)); |
| 91 | + |
| 92 | + zend_object_value retval; |
| 93 | + zend_object_std_init(&(objval->obj), ce TSRMLS_CC); |
| 94 | + |
| 95 | + retval.handle = zend_objects_store_put(objval , |
| 96 | + NULL, |
| 97 | + (zend_objects_free_object_storage_t)hello_dtor, |
| 98 | + NULL TSRMLS_CC); |
| 99 | + retval.handlers = &hello_handlers; |
| 100 | + |
| 101 | + return retval; |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +static zend_object_value hello_ctor_ex(hello_object **pobjval, zend_class_entry *ce TSRMLS_DC) { |
| 106 | + hello_object *objval = emalloc(sizeof(hello_object)); |
| 107 | + memset(objval, 0, sizeof(hello_object)); |
| 108 | + |
| 109 | + zend_object_value retval; |
| 110 | + zend_object_std_init(&(objval->obj), ce TSRMLS_CC); |
| 111 | + |
| 112 | + retval.handle = zend_objects_store_put(objval, |
| 113 | + NULL, |
| 114 | + (zend_objects_free_object_storage_t) hello_dtor, |
| 115 | + NULL TSRMLS_CC); |
| 116 | + retval.handlers = &hello_handlers; |
| 117 | + |
| 118 | + if (pobjval) { |
| 119 | + *pobjval = objval; |
| 120 | + } |
| 121 | + |
| 122 | + return retval; |
| 123 | +} |
| 124 | + |
| 125 | +static zend_object_value hello_clone(zval *zobject TSRMLS_DC) { |
| 126 | + hello_object *old_object = HELLO_FETCH_OBJECT(zobject); |
| 127 | + hello_object *new_object; |
| 128 | + |
| 129 | + zend_object_value retval = hello_ctor_ex(&new_object, old_object->obj.ce TSRMLS_CC); |
| 130 | + |
| 131 | + zend_objects_clone_members(&(new_object->obj), retval, &(old_object->obj), Z_OBJ_HANDLE_P(zobject) TSRMLS_CC); |
| 132 | + |
| 133 | + if (old_object->name) { |
| 134 | + new_object->name = estrndup(old_object->name, old_object->name_len); |
| 135 | + new_object->name_len = old_object->name_len; |
| 136 | + } |
| 137 | + new_object->age = old_object->age; |
| 138 | + |
| 139 | + return retval; |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +static zval *hello_get(zval *zobject TSRMLS_CC) { |
| 144 | + //hello_object *ho = HELLO_FETCH_OBJECT(zobject); |
| 145 | + zval *val; |
| 146 | + MAKE_STD_ZVAL(val); |
| 147 | + ZVAL_STRING(val, "bar", 1); |
| 148 | + |
| 149 | + return val; |
| 150 | +} |
| 151 | + |
| 152 | +static int hello_count_elements(zval *object, long *count TSRMLS_DC) { |
| 153 | + zend_object_handlers *zh = zend_get_std_object_handlers(); |
| 154 | + php_printf("count: %ld\n", *count); |
| 155 | + *count = 5; |
| 156 | + return SUCCESS; |
| 157 | +} |
| 158 | + |
| 159 | +PHP_MINIT_FUNCTION(objects3){ |
| 160 | + zend_class_entry ce; |
| 161 | + INIT_CLASS_ENTRY(ce, "Hello", hello_methods) |
| 162 | + |
| 163 | + hello_ce = zend_register_internal_class(&ce TSRMLS_CC); |
| 164 | + hello_ce->create_object = hello_ctor; |
| 165 | + |
| 166 | + memcpy(&hello_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 167 | + hello_handlers.clone_obj = hello_clone; |
| 168 | + //hello_handlers.get = hello_get; |
| 169 | + hello_handlers.count_elements = hello_count_elements; |
| 170 | + return SUCCESS; |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +zend_module_entry objects3_module_entry = { |
| 175 | + STANDARD_MODULE_HEADER, |
| 176 | + "objects3", /* extname */ |
| 177 | + NULL, /* functions */ |
| 178 | + PHP_MINIT(objects3), |
| 179 | + NULL, /* MSHUTDOWN */ |
| 180 | + NULL, /* RINIT */ |
| 181 | + NULL, /* RSHUTDOWN */ |
| 182 | + NULL, /* MINFO */ |
| 183 | + NO_VERSION_YET, |
| 184 | + STANDARD_MODULE_PROPERTIES |
| 185 | +}; |
| 186 | + |
| 187 | +#ifdef COMPILE_DL_OBJECTS3 |
| 188 | +ZEND_GET_MODULE(objects3) |
| 189 | +#endif |
| 190 | + |
| 191 | + |
| 192 | +/* |
| 193 | + * Local variables: |
| 194 | + * tab-width: 4 |
| 195 | + * c-basic-offset: 4 |
| 196 | + * End: |
| 197 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 198 | + * vim<600: noet sw=4 ts=4 |
| 199 | + */ |
0 commit comments