rfc:readonly_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:readonly_classes [2021/11/22 15:46] kocsismaterfc: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 <kocsismate@php.net>   * Author: Máté Kocsis <kocsismate@php.net>
-  * Status: Draft+  * Status: Accepted
   * Target Version: PHP 8.2   * Target Version: PHP 8.2
   * Implementation: https://github.com/php/php-src/pull/7305   * Implementation: https://github.com/php/php-src/pull/7305
Line 20: Line 20:
 </PHP> </PHP>
  
-Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, it will prevent the usage of dynamic properties.+Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, it will prevent the creation of dynamic properties.
  
 <PHP> <PHP>
Line 34: Line 34:
 $foo = new Foo(); $foo = new Foo();
 $foo->bar = 2; $foo->bar = 2;
-// Cannot modify readonly property Foo::$bar+// Fatal Error: Uncaught Error: Cannot modify readonly property Foo::$bar
  
 $foo->baz = 1; $foo->baz = 1;
-// Cannot create dynamic property Foo::$baz+// Fatal Error: Uncaught Error: Cannot create dynamic property Foo::$baz 
 +</PHP>
  
 +[[rfc:deprecate_dynamic_properties|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:
 +
 +<PHP>
 +#[AllowDynamicProperties]
 +readonly class Foo {
 +}
 +
 +// Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class Foo
 </PHP> </PHP>
 +
  
 ==== Restrictions ==== ==== Restrictions ====
  
 As neither untyped, nor static properties are covered by the [[rfc:readonly_properties_v2#restrictions|Readonly properties RFC]], As neither untyped, nor static properties are covered by the [[rfc:readonly_properties_v2#restrictions|Readonly properties RFC]],
-readonly classes cannot declare any of them:+readonly classes cannot declare them either:
  
 <PHP> <PHP>
Line 62: Line 72:
  
 // Fatal error: Readonly class Foo cannot declare static properties // Fatal error: Readonly class Foo cannot declare static properties
 +</PHP>
  
 ==== Inheritance ==== ==== Inheritance ====
  
-Similarly how overriding of readonly properties workonly a readonly class can extend a readonly parent:+Similarly how overriding of readonly properties works, a readonly class can only extend a readonly parent:
  
 <PHP> <PHP>
-class A {} +readonly class A {} 
-readonly class B extends A {}+readonly class B extends A {} // valid
 </PHP> </PHP>
  
-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
 </PHP> </PHP>
  
Line 83: 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
 </PHP> </PHP>
  
Line 92: Line 104:
  
 None. None.
 +
 +===== Errata =====
 +
 +  * [[https://github.com/php/php-src/issues/9285|https://github.com/php/php-src/issues/9285]]: It used to be possible to add non-readonly properties to readonly classes via traits. As on PHP 8.2 RC 1, traits cannot be used by readonly classes if they define any non-readonly property, otherwise a compilation error is emitted.
  
 ===== Vote ===== ===== Vote =====
  
-Add readonly classes as proposed?+Voted started on 2022-04-27, ending on 2022-05-11 
 + 
 +<doodle title="Add readonly classes as proposed?" auth="kocsismate" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
rfc/readonly_classes.1637595961.txt.gz · Last modified: 2021/11/22 15:46 by kocsismate