rfc:readonly_classes

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:readonly_classes [2021/08/05 08:54] – created kocsismaterfc:readonly_classes [2021/11/22 19:59] 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: Under Discussion
   * 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 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-PHP 8.1 added support for ''readonly'' properties via [[rfc:readonly_properties_v2|PHP RFC: Readonly properties 2.0]].+PHP 8.1 added support for readonly properties via [[rfc:readonly_properties_v2|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.
  
 ===== Proposal ===== ===== Proposal =====
Line 20: Line 20:
 </PHP> </PHP>
  
-Doing so will implicitly mark all typed instance properties of a class as readonly. Furthermore, it will prevent the usage of dynmacic properties.+Doing so will implicitly mark all instance properties of a class as readonly. Furthermore, it will prevent the usage of dynamic properties. 
 + 
 +<PHP> 
 +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>
  
 ==== Restrictions ==== ==== Restrictions ====
  
-A readonly class can only have typed properties.+As neither untyped, nor static properties are covered by the [[rfc:readonly_properties_v2#restrictions|Readonly properties RFC]], 
 +readonly classes cannot declare them either:
  
-Readonly static properties are not supported. This is a technical limitation, in that it is not possible to implement readonly static properties non-intrusively. In conjunction with the questionable usefulness of readonly static properties, this is not considered worthwhile at this time.+<PHP> 
 +readonly class Foo 
 +
 +    public $bar; 
 +
 + 
 +// Fatal error: Readonly property Foo::$bar must have type 
 +</PHP> 
 + 
 +<PHP> 
 +readonly class Foo 
 +
 +    public static int $bar; 
 +
 + 
 +// Fatal error: Readonly class Foo cannot declare static properties 
 +</PHP>
  
 ==== Inheritance ==== ==== Inheritance ====
  
-Similarly how overriding of readonly properties work, only a readonly class can extend a readonly class:+Similarly how overriding of readonly properties works, only a readonly class can extend a readonly parent:
  
 <PHP> <PHP>
Line 42: Line 77:
 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 47: Line 83:
 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>
  
 ==== Reflection ==== ==== Reflection ====
  
-A ''ReflectionClass::isReadOnly()'' method is added, which reports whether a class is declared as read-only. ''ReflectionClass::getModifiers()'' will also report a ''ReflectionClass::IS_READONLY'' flag.+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.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/readonly_classes.txt · Last modified: 2022/08/21 08:47 by kocsismate