helly Wed Jul 2 03:24:11 2003 EDT Modified files: /ZendEngine2 zend_objects.c /php-src/tests/classes clone_002.phpt clone_003.phpt Log: Finally fix property cloning and fix the tests accordingly. # The default behaviour is to copy all properties with all current values # from the old object. But if __clone is overwritten then only the default # properties are cloned with their correct default values. So we keep # the type system intact and also allow real __clone overwriting now. Index: ZendEngine2/zend_objects.c diff -u ZendEngine2/zend_objects.c:1.31 ZendEngine2/zend_objects.c:1.32 --- ZendEngine2/zend_objects.c:1.31 Tue Jul 1 19:29:36 2003 +++ ZendEngine2/zend_objects.c Wed Jul 2 03:24:10 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_objects.c,v 1.31 2003/07/01 23:29:36 helly Exp $ */ +/* $Id: zend_objects.c,v 1.32 2003/07/02 07:24:10 helly Exp $ */ #include "zend.h" #include "zend_globals.h" @@ -108,12 +108,13 @@ zend_object *new_object; zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); + /* assume that create isn't overwritten, so when clone depends on the + * overwritten one then it must itself be overwritten */ old_object = zend_objects_get_address(zobject TSRMLS_CC); retval = zend_objects_new(&new_object, old_object->ce TSRMLS_CC); ALLOC_HASHTABLE(new_object->properties); zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(new_object->properties, old_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) NULL /* Not used anymore */, sizeof(zval *)); if (old_object->ce->clone) { zval *old_obj; @@ -121,6 +122,9 @@ zval *clone_func_name; zval *retval_ptr; HashTable symbol_table; + zend_class_entry *ce = old_object->ce; + + zend_hash_copy(new_object->properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); MAKE_STD_ZVAL(new_obj); new_obj->type = IS_OBJECT; @@ -148,6 +152,8 @@ zval_ptr_dtor(&new_obj); zval_ptr_dtor(&clone_func_name); zval_ptr_dtor(&retval_ptr); + } else { + zend_hash_copy(new_object->properties, old_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) NULL /* Not used anymore */, sizeof(zval *)); } return retval; Index: php-src/tests/classes/clone_002.phpt diff -u php-src/tests/classes/clone_002.phpt:1.3 php-src/tests/classes/clone_002.phpt:1.4 --- php-src/tests/classes/clone_002.phpt:1.3 Tue Jul 1 19:20:48 2003 +++ php-src/tests/classes/clone_002.phpt Wed Jul 2 03:24:11 2003 @@ -38,7 +38,7 @@ ["p1"]=> int(1) ["p2"]=> - string(1) "A" + int(2) ["p3"]=> string(1) "C" } Index: php-src/tests/classes/clone_003.phpt diff -u php-src/tests/classes/clone_003.phpt:1.1 php-src/tests/classes/clone_003.phpt:1.2 --- php-src/tests/classes/clone_003.phpt:1.1 Tue Jul 1 19:57:27 2003 +++ php-src/tests/classes/clone_003.phpt Wed Jul 2 03:24:11 2003 @@ -5,26 +5,29 @@ --FILE-- <?php class base { - private $p1 = 1; - protected $p2 = 2; - public $p3; + protected $p1 = 'base:1'; + public $p2 = 'base:2'; + public $p3 = 'base:3'; + public $p4 = 'base:4'; + public $p5 = 'base:5'; + private $p6 = 'base:6'; public function __clone() { } }; -class test { - public $p1 = 4; - protected $p4 = 5; - public $p5; +class test extends base { + public $p1 = 'test:1'; + public $p3 = 'test:3'; + public $p4 = 'test:4'; + public $p5 = 'test:5'; public function __clone() { + $this->p5 = 'clone:5'; } } $obj = new test; -$obj->p2 = 'A'; -$obj->p3 = 'B'; +$obj->p4 = 'A'; $copy = $obj->__clone(); -$copy->p3 = 'C'; echo "Object\n"; print_r($obj); echo "Clown\n"; @@ -35,20 +38,23 @@ Object test Object ( - [p1] => 4 - [p4:protected] => 5 - [p5] => - [p2] => A - [p3] => B + [p1] => test:1 + [p3] => test:3 + [p4] => A + [p5] => test:5 + [p1:protected] => base:1 + [p2] => base:2 + [p6:private] => base:6 ) Clown test Object ( - [p1] => 4 - [p4:protected] => 5 - [p5] => - [p1:private] => 1 - [p2] => A - [p3] => C + [p1] => test:1 + [p3] => test:3 + [p4] => test:4 + [p5] => clone:5 + [p1:protected] => base:1 + [p2] => base:2 + [p6:private] => base:6 ) Done
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php