Skip to content

Commit 04a5f2b

Browse files
committed
Add test to make sure that readonly properties cannot be reassigned by invoking the __clone() method directly
1 parent eee6509 commit 04a5f2b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Test that readonly properties cannot be reassigned by invoking the __clone() method directly
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function __construct(
9+
public readonly int $bar
10+
) {}
11+
12+
public function __clone()
13+
{
14+
$this->bar = 1;
15+
}
16+
}
17+
18+
$foo = new Foo(0);
19+
20+
var_dump($foo);
21+
22+
try {
23+
$foo->__clone();
24+
} catch (Error $e) {
25+
echo $e->getMessage() . "\n";
26+
}
27+
28+
try {
29+
$foo->__clone();
30+
} catch (Error $e) {
31+
echo $e->getMessage() . "\n";
32+
}
33+
34+
var_dump($foo);
35+
36+
?>
37+
--EXPECTF--
38+
object(Foo)#%d (%d) {
39+
["bar"]=>
40+
int(0)
41+
}
42+
Cannot modify readonly property Foo::$bar
43+
Cannot modify readonly property Foo::$bar
44+
object(Foo)#%d (%d) {
45+
["bar"]=>
46+
int(0)
47+
}

0 commit comments

Comments
 (0)