Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Zend/tests/clone/clone_with_014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Properties are still readonly after clone-with
--FILE--
<?php

readonly class Test {
public public(set) int $a;
public public(set) int $b;

public function __construct() {
$this->a = 1;
$this->b = 2;
}
}

$test = clone(new Test(), ['a' => 3]);
var_dump($test);

try {
$test->b = 4;
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}

var_dump($test);

?>
--EXPECT--
object(Test)#2 (2) {
["a"]=>
int(3)
["b"]=>
int(2)
}
Error: Cannot modify readonly property Test::$b
object(Test)#2 (2) {
["a"]=>
int(3)
["b"]=>
int(2)
}
8 changes: 8 additions & 0 deletions Zend/zend_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const
} ZEND_HASH_FOREACH_END();

EG(fake_scope) = old_scope;

/* Lock readonly properties once more. */
if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
zval* prop = OBJ_PROP_NUM(new_object, i);
Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
}
}
}

return new_object;
Expand Down
Loading