xref: /PHP-8.4/Zend/tests/gh10377.phpt (revision e52684ea)
1--TEST--
2GH-10377 (Unable to have an anonymous readonly class)
3--FILE--
4<?php
5
6$readonly_anon = new readonly class {
7    public int $field;
8    function __construct() {
9        $this->field = 2;
10    }
11};
12
13$anon = new class {
14    public int $field;
15    function __construct() {
16        $this->field = 2;
17    }
18};
19
20var_dump($readonly_anon->field);
21try {
22    $readonly_anon->field = 123;
23} catch (Error $e) {
24    echo $e->getMessage() . "\n";
25}
26var_dump($readonly_anon->field);
27
28var_dump($anon->field);
29try {
30    $anon->field = 123;
31} catch (Error $e) {
32    echo $e->getMessage() . "\n";
33}
34var_dump($anon->field);
35
36?>
37--EXPECT--
38int(2)
39Cannot modify readonly property class@anonymous::$field
40int(2)
41int(2)
42int(123)
43