1
0
mirror of https://github.com/php/php-src.git synced 2026-04-25 08:58:28 +02:00
Files
archived-php-src/Zend/tests/gh10377.phpt
T
Niels Dossche e52684ea8a Fix GH-10377: Unable to have an anonymous readonly class
This fixes the oversight that an anonymous class should be able to be
readonly. Other identifiers such as final and abstract do not make
sense. As we still want nice errors for when users try to use these
modifiers, or use multiple modifiers, we introduce a new function
zend_add_anonymous_class_modifier that will perform verification for
anonymous class modifiers, just like zend_add_class_modifier does for
non-anonymous classes.

Closes GH-10381
2023-02-20 13:32:26 +01:00

43 lines
712 B
PHP

--TEST--
GH-10377 (Unable to have an anonymous readonly class)
--FILE--
<?php
$readonly_anon = new readonly class {
public int $field;
function __construct() {
$this->field = 2;
}
};
$anon = new class {
public int $field;
function __construct() {
$this->field = 2;
}
};
var_dump($readonly_anon->field);
try {
$readonly_anon->field = 123;
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($readonly_anon->field);
var_dump($anon->field);
try {
$anon->field = 123;
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($anon->field);
?>
--EXPECT--
int(2)
Cannot modify readonly property class@anonymous::$field
int(2)
int(2)
int(123)