php_property_proxy_t *proxy = ecalloc(1, sizeof(*proxy));
ZVAL_COPY(&proxy->container, get_referenced_zval(container));
- zend_string_addref(member);
- proxy->member = member;
+ proxy->member = zend_string_copy(member);
return proxy;
}
void php_property_proxy_free(php_property_proxy_t **proxy)
{
if (*proxy) {
- zval_ptr_dtor(&(*proxy)->container);
+ zval_ptr_dtor( &(*proxy)->container);
zend_string_release((*proxy)->member);
efree(*proxy);
*proxy = NULL;
}
}
+zval *php_property_proxy_zval(zval *container, zend_string *member)
+{
+ php_property_proxy_t *proxy;
+ php_property_proxy_object_t *proxy_obj;
+
+ proxy = php_property_proxy_init(container, member);
+ proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
+
+ ZVAL_OBJ(&proxy_obj->myself, &proxy_obj->zo);
+ Z_ADDREF(proxy_obj->myself);
+ return &proxy_obj->myself;
+}
+
static zend_class_entry *php_property_proxy_class_entry;
static zend_object_handlers php_property_proxy_object_handlers;
#define debug_propro(l, f, obj, off, val)
#endif
-static php_property_proxy_object_t *new_propro(zend_class_entry *ce,
- php_property_proxy_t *proxy)
+php_property_proxy_object_t *php_property_proxy_object_new_ex(
+ zend_class_entry *ce, php_property_proxy_t *proxy)
{
php_property_proxy_object_t *o;
return o;
}
-static zend_object *create_obj(zend_class_entry *ce)
+zend_object *php_property_proxy_object_new(zend_class_entry *ce)
{
- return &new_propro(ce, NULL)->zo;
+ return &php_property_proxy_object_new_ex(ce, NULL)->zo;
}
static void destroy_obj(zend_object *object)
zval_ptr_dtor(&o->parent);
ZVAL_UNDEF(&o->parent);
}
+ zend_object_std_dtor(object);
}
static inline php_property_proxy_object_t *get_propro(zval *object)
switch (Z_TYPE_P(ref)) {
case IS_OBJECT:
zend_update_property(Z_OBJCE_P(ref), ref, obj->proxy->member->val,
- obj->proxy->member->len, value TSRMLS_CC);
+ obj->proxy->member->len, value);
break;
default:
zend_string_release(member);
}
- proxy_obj = new_propro(NULL, proxy);
+ proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
ZVAL_COPY(&proxy_obj->parent, object);
RETVAL_OBJ(&proxy_obj->zo);
}
if (Z_ISUNDEF(proxied_value)) {
exists = 0;
} else {
- zval *o = offset;
-
- convert_to_string_ex(o);
+ zend_string *zs = zval_get_string(offset);
if (Z_TYPE(proxied_value) == IS_ARRAY) {
- zval *zentry = zend_symtable_find(Z_ARRVAL(proxied_value), Z_STR_P(o));
+ zval *zentry = zend_symtable_find(Z_ARRVAL(proxied_value), zs);
if (!zentry) {
exists = 0;
}
}
- if (o != offset) {
- zval_ptr_dtor(o);
- }
+ zend_string_release(zs);
}
debug_propro(-1, "dim_exists", get_propro(object), offset, NULL);
static void write_dimension(zval *object, zval *offset, zval *value)
{
- zval proxied_value, *o = offset;
+ zval proxied_value;
debug_propro(1, "dim_write", get_propro(object), offset, value);
SEPARATE_ZVAL(value);
Z_TRY_ADDREF_P(value);
- if (o) {
- convert_to_string_ex(o);
- zend_symtable_update(Z_ARRVAL(proxied_value), Z_STR_P(o), value);
+ if (offset) {
+ zend_string *zs = zval_get_string(offset);
+ zend_symtable_update(Z_ARRVAL(proxied_value), zs, value);
+ zend_string_release(zs);
} else {
zend_hash_next_index_insert(Z_ARRVAL(proxied_value), value);
}
- if (o && o != offset) {
- zval_ptr_dtor(o);
- }
-
set_proxied_value(object, &proxied_value);
debug_propro(-1, "dim_write", get_propro(object), offset, &proxied_value);
debug_propro(1, "dim_unset", get_propro(object), offset, NULL);
- get_proxied_value(object, &proxied_value TSRMLS_CC);
+ ZVAL_UNDEF(&proxied_value);
+ get_proxied_value(object, &proxied_value);
if (Z_TYPE(proxied_value) == IS_ARRAY) {
zval *o = offset;
INIT_NS_CLASS_ENTRY(ce, "php", "PropertyProxy",
php_property_proxy_method_entry);
php_property_proxy_class_entry = zend_register_internal_class(&ce);
- php_property_proxy_class_entry->create_object = create_obj;
+ php_property_proxy_class_entry->create_object = php_property_proxy_object_new;
php_property_proxy_class_entry->ce_flags |= ZEND_ACC_FINAL;
memcpy(&php_property_proxy_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
php_property_proxy_object_handlers.offset = XtOffsetOf(php_property_proxy_object_t, zo);
- php_property_proxy_object_handlers.dtor_obj = destroy_obj;
+ php_property_proxy_object_handlers.free_obj = destroy_obj;
php_property_proxy_object_handlers.set = set_proxied_value;
php_property_proxy_object_handlers.get = get_proxied_value;
php_property_proxy_object_handlers.cast_object = cast_proxied_value;
*
* Example:
* ~~~~~~~~~~{.c}
- * static zval *my_read_prop(zval *object, zval *member, int type, zend_literal *key TSRMLS_DC)
+ * static zval *my_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
* {
- * my_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
- * my_prophandler_t *handler;
- * zval *return_value, *copy = my_cast(IS_STRING, member);
+ * zval *return_value;
+ * zend_string *member_name = zval_get_string(member);
+ * my_prophandler_t *handler = my_get_prophandler(member_name);
*
- * if (SUCCESS == my_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
- * ALLOC_ZVAL(return_value);
- * Z_SET_REFCOUNT_P(return_value, 0);
- * Z_UNSET_ISREF_P(return_value);
+ * if (!handler || type == BP_VAR_R || type == BP_VAR_IS) {
+ * return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
*
- * if (type == BP_VAR_R) {
- * handler->read(obj, return_value TSRMLS_CC);
- * } else {
- * //
- * // This is the interesting part
- * //
- * php_property_proxy_t *proxy;
- * zend_object_value proxy_ov;
- * zend_class_entry *proxy_ce;
+ * if (handler) {
+ * handler->read(object, tmp);
*
- * proxy = php_property_proxy_init(object, Z_STRVAL_P(copy), Z_STRLEN_P(copy) TSRMLS_CC);
- * proxy_ce = php_property_proxy_get_class_entry();
- * proxy_ov = php_property_proxy_object_new_ex(proxy_ce, proxy, NULL TSRMLS_CC);
- * RETVAL_OBJVAL(proxy_ov, 0);
- * }
- * } else {
- * zend_object_handlers *oh = zend_get_std_object_handlers();
- * return_value = oh->read_property(object, member, type, key TSRMLS_CC);
- * }
+ * zval_ptr_dtor(return_value);
+ * ZVAL_COPY_VALUE(return_value, tmp);
+ * }
+ * } else {
+ * return_value = php_property_proxy_zval(object, member_name);
+ * }
*
- * zval_ptr_dtor(©);
+ * zend_string_release(member_name);
*
- * return return_value;
+ * return return_value;
* }
* ~~~~~~~~~~
*/
php_property_proxy_t *proxy;
/** Any parent property proxy object */
zval parent;
+ /** Bond, James Bond */
+ zval myself;
/** The std zend_object */
zend_object zo;
};
typedef struct php_property_proxy_object php_property_proxy_object_t;
+PHP_PROPRO_API php_property_proxy_object_t *php_property_proxy_object_new_ex(
+ zend_class_entry *ce, php_property_proxy_t *proxy);
+
+PHP_PROPRO_API zend_object *php_property_proxy_object_new(zend_class_entry *ce);
+
+/**
+ * Create a property proxy as zval suitable to return from the property handler.
+ *
+ * Wrapper for php_property_proxy_init() and php_property_proxy_object_new_ex()
+ * for use within a custom property handler.
+ *
+ * @param container the container holding the property
+ * @param member the name of the proxied property
+ * @return the new property proxy as zval
+ */
+PHP_PROPRO_API zval *php_property_proxy_zval(zval *container, zend_string *member);
+
/**
* Create a property proxy
*