Skip to content

Commit c9f9913

Browse files
committed
Fix some tests + add one
1 parent 75feb75 commit c9f9913

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Test that using "clone with" with deep cloned properties will result in the property to be cloned twice
3+
--FILE--
4+
<?php
5+
6+
class Bar
7+
{
8+
public function __construct(
9+
private readonly int $baz
10+
) {
11+
}
12+
13+
public function __clone(): void
14+
{
15+
echo "Cloning Bar\n";
16+
}
17+
}
18+
19+
class Foo
20+
{
21+
public function __construct(
22+
public readonly Bar $bar
23+
) {}
24+
25+
public function __clone(): void
26+
{
27+
echo "Cloning Foo\n";
28+
$this->bar = clone $this->bar;
29+
}
30+
31+
public function withBar(): static
32+
{
33+
return clone $this with [
34+
"bar" => new Bar(2),
35+
];
36+
}
37+
}
38+
39+
$foo = new Foo(new Bar(1));
40+
41+
var_dump($foo->withBar());
42+
43+
?>
44+
--EXPECTF--
45+
Cloning Foo
46+
Cloning Bar
47+
object(Foo)#3 (1) {
48+
["bar"]=>
49+
object(Bar)#5 (1) {
50+
["baz":"Bar":private]=>
51+
int(2)
52+
}
53+
}

Zend/zend_object_handlers.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,10 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
825825
bool is_clone_with_op = false;
826826

827827
if (UNEXPECTED(is_readonly)) {
828-
is_clone_with_op = EG(current_execute_data) && EG(current_execute_data)->opline &&
828+
is_clone_with_op = EG(current_execute_data) &&
829+
EG(current_execute_data)->func &&
830+
ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
831+
EG(current_execute_data)->opline &&
829832
EG(current_execute_data)->opline->opcode == ZEND_CLONE_INIT_PROP;
830833

831834
if (UNEXPECTED(

0 commit comments

Comments
 (0)