作者:小鬼6Pi7 | 来源:互联网 | 2023-10-12 11:57
Iamtryingtoimplementtheprototypedesignpatterninmyextension.HowcanIcloneanobjectfro
I am trying to implement the prototype design pattern in my extension. How can I clone an object from an zval*
. Is there a zend function equivalent to the userland clone
operator?
我正在尝试在我的扩展中实现原型设计模式。如何从zval *克隆对象?是否有与用户态克隆运算符等效的zend函数?
Update
更新
I wrote the following function for cloning an object. Not sure if there is a better way.
我编写了以下用于克隆对象的函数。不确定是否有更好的方法。
/**
* Clones an object from src to dest
*/
static int php_custom_clone(zval *src, zval *dest TSRMLS_DC) {
zend_class_entry *ce;
zend_object_clone_obj_t clone_call;
ce = Z_OBJCE_P(src);
clone_call = Z_OBJ_HT_P(src)->clone_obj;
if (!clone_call) {
if (ce) {
zend_throw_exception_ex(Custom_Exception_ce_ptr, 0 TSRMLS_CC, "Trying to clone an uncloneable object of class %s", ce->name);
} else {
zend_throw_exception_ex(Custom_Exception_ce_ptr, 0 TSRMLS_CC, "Trying to clone an uncloneable object");
}
} else {
if (!EG(exception)) {
Z_OBJVAL_P(dest) = clone_call(src TSRMLS_CC);
Z_TYPE_P(dest) = IS_OBJECT;
Z_SET_REFCOUNT_P(dest, 1);
// Z_SET_ISREF_P(dest);
if (EG(exception)) {
zval_ptr_dtor(&dest);
} else {
return 1;
}
}
}
return 0;
}
1 个解决方案