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 06:51] – 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 '' | + | PHP 8.1 added support for readonly properties via [[rfc: |
===== Proposal ===== | ===== Proposal ===== | ||
Line 20: | Line 20: | ||
</ | </ | ||
- | Doing so will implicitly mark all typed instance properties of a class as readonly. Furthermore, | + | Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, |
+ | |||
+ | < | ||
+ | readonly class Foo | ||
+ | { | ||
+ | public int $bar; | ||
+ | |||
+ | public function __construct() { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $foo = new Foo(); | ||
+ | $foo-> | ||
+ | // Fatal Error: Uncaught Error: Cannot modify readonly property Foo::$bar | ||
+ | |||
+ | $foo-> | ||
+ | // Fatal Error: Uncaught Error: Cannot create dynamic property Foo::$baz | ||
+ | </ | ||
+ | |||
+ | [[rfc: | ||
+ | |||
+ | < | ||
+ | # | ||
+ | readonly class Foo { | ||
+ | } | ||
+ | |||
+ | // Fatal error: Cannot apply # | ||
+ | </ | ||
==== Restrictions ==== | ==== Restrictions ==== | ||
- | A readonly class can only have typed properties. | + | As neither untyped, nor static |
+ | readonly classes cannot declare them either: | ||
- | As readonly | + | < |
+ | readonly | ||
+ | { | ||
+ | public $bar; | ||
+ | } | ||
+ | |||
+ | // Fatal error: Readonly property Foo::$bar must have type | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | readonly | ||
+ | { | ||
+ | public static int $bar; | ||
+ | } | ||
+ | |||
+ | // Fatal error: Readonly class Foo cannot | ||
+ | </ | ||
==== 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 47: | 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 | ||
</ | </ | ||
==== Reflection ==== | ==== Reflection ==== | ||
- | A '' | + | A '' |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
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.1637563870.txt.gz · Last modified: 2021/11/22 06:51 by kocsismate