mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
Add support for readonly properties, for which only a single initializing assignment from the declaring scope is allowed. RFC: https://wiki.php.net/rfc/readonly_properties_v2 Closes GH-7089.
30 lines
461 B
PHP
30 lines
461 B
PHP
--TEST--
|
|
By-ref foreach over readonly property
|
|
--FILE--
|
|
<?php
|
|
|
|
class Test {
|
|
public readonly int $prop;
|
|
|
|
public function init() {
|
|
$this->prop = 1;
|
|
}
|
|
}
|
|
|
|
$test = new Test;
|
|
|
|
// Okay, as foreach skips over uninitialized properties.
|
|
foreach ($test as &$prop) {}
|
|
|
|
$test->init();
|
|
|
|
try {
|
|
foreach ($test as &$prop) {}
|
|
} catch (Error $e) {
|
|
echo $e->getMessage(), "\n";
|
|
}
|
|
|
|
?>
|
|
--EXPECT--
|
|
Cannot acquire reference to readonly property Test::$prop
|