mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Add test to make sure that readonly properties cannot be reassigned by invoking the __clone() method directly
This commit is contained in:
parent
eee650971b
commit
04a5f2b11f
1 changed files with 47 additions and 0 deletions
47
Zend/tests/readonly_props/readonly_clone_error6.phpt
Normal file
47
Zend/tests/readonly_props/readonly_clone_error6.phpt
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue