Add test to make sure that readonly properties cannot be reassigned by invoking the __clone() method directly

This commit is contained in:
Máté Kocsis 2023-04-25 17:17:05 +02:00
parent eee650971b
commit 04a5f2b11f
No known key found for this signature in database
GPG key ID: FD055E41728BF310

View file

@ -0,0 +1,47 @@
--TEST--
Test that readonly properties cannot be reassigned by invoking the __clone() method directly
--FILE--
<?php
class Foo
{
public function __construct(
public readonly int $bar
) {}
public function __clone()
{
$this->bar = 1;
}
}
$foo = new Foo(0);
var_dump($foo);
try {
$foo->__clone();
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
try {
$foo->__clone();
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($foo);
?>
--EXPECTF--
object(Foo)#%d (%d) {
["bar"]=>
int(0)
}
Cannot modify readonly property Foo::$bar
Cannot modify readonly property Foo::$bar
object(Foo)#%d (%d) {
["bar"]=>
int(0)
}