| asgrim | Yes | 2022-04-27 09:12 |
| ashnazg | Yes | 2022-04-27 20:18 |
| beberlei | Yes | 2022-04-28 08:37 |
| bukka | Yes | 2022-04-27 17:43 |
| bwoebi | No | 2022-05-01 16:25 |
| crell | Yes | 2022-04-27 15:06 |
| cschneid | No | 2022-04-27 07:42 |
| derick | Yes | 2022-04-27 12:28 |
| didou | Yes | 2022-04-27 07:35 |
| diegopires | Yes | 2022-04-28 12:18 |
| dmitry | No | 2022-04-28 05:11 |
| dragoonis | Yes | 2022-04-28 11:48 |
| galvao | Yes | 2022-04-28 11:21 |
| geekcom | Yes | 2022-04-28 13:28 |
| girgias | Yes | 2022-04-27 15:43 |
| jbnahan | Yes | 2022-04-27 07:24 |
| jhdxr | No | 2022-04-28 08:01 |
| kocsismate | Yes | 2022-04-27 07:05 |
| lcobucci | Yes | 2022-04-29 08:38 |
| lstrojny | Yes | 2022-04-27 07:11 |
| lufei | Yes | 2022-05-10 08:48 |
| mauricio | Yes | 2022-05-03 18:13 |
| mgocobachi | Yes | 2022-04-29 02:17 |
| nicolasgrekas | Yes | 2022-04-27 11:27 |
| ocramius | Yes | 2022-04-27 08:39 |
| pierrick | Yes | 2022-05-05 22:40 |
| pollita | No | 2022-04-27 13:40 |
| reywob | No | 2022-04-27 08:11 |
| santiagolizardo | Yes | 2022-05-01 20:53 |
| sebastian | Yes | 2022-04-28 08:57 |
| sergey | Yes | 2022-04-27 13:59 |
| stas | No | 2022-05-10 03:08 |
| theodorejb | Yes | 2022-05-01 14:54 |
| weierophinney | Yes | 2022-05-03 21:42 |
| wyrihaximus | Yes | 2022-04-28 13:18 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC
PHP 8.1 added support for readonly properties via PHP RFC: Readonly properties 2.0. However, it's still not easy to declare (quasi-)immutable classes, especially if they contain many properties. Therefore, this RFC proposes to add support for readonly classes.
The usage of the readonly modifier added by PHP RFC: Readonly properties 2.0 is extended to classes:
readonly class Test {
public string $prop;
}
Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, it will prevent the creation of dynamic properties.
readonly class Foo
{
public int $bar;
public function __construct() {
$this->bar = 1;
}
}
$foo = new Foo();
$foo->bar = 2;
// Fatal Error: Uncaught Error: Cannot modify readonly property Foo::$bar
$foo->baz = 1;
// Fatal Error: Uncaught Error: Cannot create dynamic property Foo::$baz
PHP RFC: Deprecate dynamic properties added support for the #[AllowDynamicProperties] attribute which makes it possible to create dynamic properties without triggering errors. In order not to violate the read-only constraint, marking readonly classes with the above attribute is a compile-time error:
#[AllowDynamicProperties]
readonly class Foo {
}
// Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class Foo
As neither untyped, nor static properties are covered by the Readonly properties RFC, readonly classes cannot declare them either:
readonly class Foo
{
public $bar;
}
// Fatal error: Readonly property Foo::$bar must have type
readonly class Foo
{
public static int $bar;
}
// Fatal error: Readonly class Foo cannot declare static properties
Similarly how overriding of readonly properties works, a readonly class can only extend a readonly parent:
readonly class A {}
readonly class B extends A {} // valid
But both of the following are illegal:
readonly class A {}
class B extends A {}
// Fatal error: Non-readonly class B cannot extend readonly class A
class A {}
readonly class B extends A {}
// Fatal error: Readonly class B cannot extend non-readonly class A
A ReflectionClass::isReadOnly() method is added, which reports whether a class is declared as read-only. Additionally, ReflectionClass::getModifiers() will also include the ReflectionClass::IS_READONLY flag.
None.
Voted started on 2022-04-27, ending on 2022-05-11
Vote: Add readonly classes as proposed? — Yes: 28 · No: 7 (closed)