rfc:readonly_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:readonly_classes [2021/11/22 15:42] – kocsismate | rfc:readonly_classes [2022/04/19 07:09] – 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 34: | Line 34: | ||
$foo = new Foo(); | $foo = new Foo(); | ||
$foo-> | $foo-> | ||
- | // Cannot modify readonly property Foo::$bar | + | // Fatal Error: Uncaught Error: |
$foo-> | $foo-> | ||
- | // Cannot create dynamic property Foo::$baz | + | // Fatal Error: Uncaught Error: |
</ | </ | ||
==== 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 {} | class A {} | ||
- | readonly class B extends A {} | + | readonly class B extends A {} // valid |
+ | |||
+ | readonly class C {} | ||
+ | readonly class C extends B {} // 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 66: | Line 86: | ||
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 | ||
</ | </ | ||
rfc/readonly_classes.txt · Last modified: 2022/08/21 08:47 by kocsismate