rfc:readonly_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:readonly_classes [2021/11/22 15:40] – kocsismate | rfc:readonly_classes [2022/08/21 08:47] (current) – kocsismate | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2021-08-04 | * Date: 2021-08-04 | ||
* Author: Máté Kocsis < | * Author: Máté Kocsis < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.2 | * Target Version: PHP 8.2 | ||
* Implementation: | * Implementation: | ||
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP 8.1 added support for readonly properties via [[rfc: | + | PHP 8.1 added support for readonly properties via [[rfc: |
===== Proposal ===== | ===== Proposal ===== | ||
Line 20: | Line 20: | ||
</ | </ | ||
- | Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, | + | Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, |
<PHP> | <PHP> | ||
Line 34: | Line 34: | ||
$foo = new Foo(); | $foo = new Foo(); | ||
$foo-> | $foo-> | ||
- | // Cannot modify readonly property Foo::$bar | + | // Fatal Error: Uncaught Error: |
+ | $foo-> | ||
+ | // Fatal Error: Uncaught Error: Cannot create dynamic property Foo::$baz | ||
</ | </ | ||
+ | |||
+ | [[rfc: | ||
+ | |||
+ | <PHP> | ||
+ | # | ||
+ | readonly class Foo { | ||
+ | } | ||
+ | |||
+ | // Fatal error: Cannot apply # | ||
+ | </ | ||
+ | |||
==== Restrictions ==== | ==== Restrictions ==== | ||
- | A readonly class can only have typed properties, since untyped properties | + | As neither |
+ | readonly | ||
- | As static properties are also not covered by the [[rfc:readonly_properties_v2# | + | < |
+ | readonly class Foo | ||
+ | { | ||
+ | public $bar; | ||
+ | } | ||
+ | |||
+ | // Fatal error: Readonly | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | readonly | ||
+ | { | ||
+ | public static int $bar; | ||
+ | } | ||
+ | |||
+ | // Fatal error: Readonly class Foo cannot declare | ||
+ | </ | ||
==== Inheritance ==== | ==== Inheritance ==== | ||
- | Similarly how overriding of readonly properties | + | Similarly how overriding of readonly properties |
<PHP> | <PHP> | ||
- | class A {} | + | readonly |
- | readonly class B extends A {} | + | readonly class B extends A {} // valid |
</ | </ | ||
- | Both of the following are illegal: | + | But both of the following are illegal: |
<PHP> | <PHP> | ||
readonly class A {} | readonly class A {} | ||
class B extends A {} | class B extends A {} | ||
+ | // Fatal error: Non-readonly class B cannot extend readonly class A | ||
</ | </ | ||
Line 63: | Line 94: | ||
class A {} | class A {} | ||
readonly class B extends A {} | readonly class B extends A {} | ||
+ | // Fatal error: Readonly class B cannot extend non-readonly class A | ||
</ | </ | ||
Line 72: | Line 104: | ||
None. | None. | ||
+ | |||
+ | ===== Errata ===== | ||
+ | |||
+ | * [[https:// | ||
===== Vote ===== | ===== Vote ===== | ||
- | Add readonly classes as proposed? | + | Voted started on 2022-04-27, ending on 2022-05-11 |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
rfc/readonly_classes.txt · Last modified: 2022/08/21 08:47 by kocsismate