rfc:readonly_and_immutable_properties
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:readonly_and_immutable_properties [2020/06/20 17:05] – formating andrerom | rfc:readonly_and_immutable_properties [2020/06/21 18:47] (current) – formating andrerom | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: Readonly and immutable properties |
* Version: 0.3.2 | * Version: 0.3.2 | ||
* Date: 2020-06-20 | * Date: 2020-06-20 | ||
Line 7: | Line 7: | ||
* Status: Draft | * Status: Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
- | * Discussion: https:// | + | * Discussion: https:// |
===== Introduction ===== | ===== Introduction ===== | ||
- | With the introduction of typed properties in PHP 7.4, properties have become far more powerful. However it is currently not possible to specify disconnected write vs read visibility for properties without having to resort to magic methods (getters and setters). This requires unnecessary boilerplate, | ||
- | This RFC resolves this issue by proposing: | + | **This is a early draft, currently looking for feedback on direction on what would make most sense to propose, especially |
- | 1. Possibility | + | |
- | 2. Readonly attribute | + | |
- | 3. Immutable attribute, and optionally immutable visibility | + | |
- | //These three proposals will be offered | + | With the introduction of typed properties in PHP 7.4, properties have become far more powerful. However it is currently not possible to specify disconnected write vs read visibility for properties, such as readonly, without having to resort to magic methods (getters and setters). For immutable semantic it's even more cumbersome. This requires unnecessary boilerplate, |
+ | |||
+ | This RFC resolves this issue by proposing a few options: | ||
+ | - Language approach: | ||
+ | - Change to make it possibility to specify write visibility disconnected from read. | ||
+ | - readonly keyword for write access, on property and class (implicit all properties) | ||
+ | - immutable keyword for write access, on property and class (implicit all properties) | ||
+ | - Attribute approach: | ||
+ | - Readonly attribute for properties, if #1.1/1.2 is accepted this is merely syntax sugar. | ||
+ | - Immutable attribute for properties, if #1.3 is accepted this is merely syntax sugar | ||
Line 27: | Line 32: | ||
private int $id; | private int $id; | ||
private string $name; | private string $name; | ||
- | | + | |
public function __construct(int $id, string $name) { | public function __construct(int $id, string $name) { | ||
$this-> | $this-> | ||
$this-> | $this-> | ||
} | } | ||
- | | + | |
public function __get($property) | public function __get($property) | ||
{ | { | ||
Line 41: | Line 46: | ||
throw new PropertyNotFoundException($property, | throw new PropertyNotFoundException($property, | ||
} | } | ||
- | | + | |
public function __set($property, | public function __set($property, | ||
{ | { | ||
Line 50: | Line 55: | ||
throw new PropertyNotFoundException($property, | throw new PropertyNotFoundException($property, | ||
} | } | ||
- | | + | |
public function __isset($property) | public function __isset($property) | ||
{ | { | ||
return property_exists($this, | return property_exists($this, | ||
} | } | ||
- | |||
public function __unset($property) | public function __unset($property) | ||
Line 70: | Line 74: | ||
<< | << | ||
public int $id; | public int $id; | ||
- | | + | |
<< | << | ||
public string $name; | public string $name; | ||
- | | + | |
+ | public function __construct(int $id, string $name) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | or just | ||
+ | |||
+ | <code php> | ||
+ | << | ||
+ | class User { | ||
+ | public int $id; | ||
+ | public string $name; | ||
public function __construct(int $id, string $name) { | public function __construct(int $id, string $name) { | ||
$this-> | $this-> | ||
Line 88: | Line 108: | ||
==== Readonly ==== | ==== Readonly ==== | ||
- | This RFC differs from [[rfc: | + | This RFC aligns with [[rfc: |
Line 94: | Line 114: | ||
- | This RFC differs from[[rfc: | + | This RFC aligns with [[rfc: |
- | This RFC does __not__ align with the semantics of the recent [[rfc: | + | This RFC does __not__ align with the semantics of the recent [[rfc: |
Line 102: | Line 122: | ||
- | This RFC does not try to solve as wider use case as the different iterations of [[rfc: | + | This RFC does not try to solve as wide use case as the different iterations of [[rfc: |
- | It's of this author' | + | However: |
- | + | - Accessors overcomplicates | |
- | Thus the author of this RFC believes | + | - There seems to be a higher need in the community |
- | + | - Everything | |
===== Proposal ===== | ===== Proposal ===== | ||
- | - TODO: the tree proposals, including how they should be manipulated with reflection | + | ==== Common semantics ==== |
+ | == References == | ||
+ | |||
+ | Attempting to pass a property value outside of allowed writable scope as a reference, results in an error. | ||
+ | |||
+ | |||
+ | ==== 1. Language Approach ==== | ||
+ | |||
+ | |||
+ | === 1.1 Language ability to set property visibility separately for write access === | ||
+ | |||
+ | This proposal adds support for enforced write visibility checks for declared properties. The following example illustrates the basic syntax: | ||
+ | |||
+ | <code php> | ||
+ | class User { | ||
+ | // Property is readonly in protected and public scope | ||
+ | public: | ||
+ | |||
+ | // Property is readonly in public scope | ||
+ | public: | ||
+ | | ||
+ | // Property is write-only in public and protected scope | ||
+ | private: | ||
+ | |||
+ | public function __construct(int $id, string $name) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The format is "< | ||
+ | |||
+ | |||
+ | == Reflection == | ||
+ | |||
+ | When using reflection, methods such as '' | ||
+ | |||
+ | However with this proposal the following existing methods will represent read visibility for cases where it differs: | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | |||
+ | And for checking separate write visibility the following methods may be used: | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | |||
+ | |||
+ | '' | ||
+ | |||
+ | //TODO: Expand this with modifier ints representing all variations and their names// | ||
+ | |||
+ | === 1.2 readonly keyword === | ||
+ | |||
+ | This proposal adds support for runtime-enforced readonly write visibility for declared properties. The following example illustrates the basic syntax: | ||
+ | |||
+ | <code php> | ||
+ | class User { | ||
+ | // Property is readonly, and can only be written to in protected scope | ||
+ | public readonly int $id; | ||
+ | | ||
+ | // Property is readonly, and can only be written to in private scope | ||
+ | protected readonly string $name; | ||
+ | |||
+ | // [assuming 1.1 is accepted] Invalid declaration (visibility is already stating property is readonly) | ||
+ | public: | ||
+ | |||
+ | public function __construct(int $id, string $name) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Keyword can also be set on class level, implicit setting it on all fields unless they have their own immutable attribute: | ||
+ | |||
+ | <code php> | ||
+ | readonly class User { | ||
+ | // Property is readonly, and can only be written to in protected scope | ||
+ | public int $id; | ||
+ | | ||
+ | // Property is readonly, and can only be written to in private scope | ||
+ | protected string $name; | ||
+ | |||
+ | // [assuming 1.1 is accepted] Invalid declaration (visibility is already stating property is readonly) | ||
+ | public: | ||
+ | |||
+ | public function __construct(int $id, string $name) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Readonly semantics == | ||
+ | |||
+ | An readonly property may only be written to in scope lower than what is define as its read+write visibility, so if visibility is public, it may only be written to in protected scope. | ||
+ | |||
+ | |||
+ | == Reflection == | ||
+ | |||
+ | When using reflection, methods such as '' | ||
+ | |||
+ | Furthermore the following method is proposed added to be able to detect readonly properties: | ||
+ | - '' | ||
+ | |||
+ | |||
+ | '' | ||
+ | |||
+ | //TODO: Expand this with specific modifier int for " | ||
+ | |||
+ | |||
+ | === 1.3 immutable keyword === | ||
+ | |||
+ | This proposal adds support for runtime-enforced immutable write visibility for declared properties. The following example illustrates the basic syntax: | ||
+ | |||
+ | <code php> | ||
+ | class User { | ||
+ | // Property is immutable, can only be written to in __construct in protected scope | ||
+ | public immutable int $id; | ||
+ | |||
+ | // [assuming 1.1 is accepted] Property is immutable, can only be written to in __construct in private scope | ||
+ | public: | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Keyword can also be set on class level, implicit setting it on all fields unless they have their own readonly attribute: | ||
+ | |||
+ | <code php> | ||
+ | immutable class User { | ||
+ | // Property is immutable, can only be written to in during construction in protected scope | ||
+ | public int $id; | ||
+ | | ||
+ | // Property is immutable, can only be written to during construction in private scope | ||
+ | protected string $email; | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Immutable semantics == | ||
+ | |||
+ | An immutable property may only be written to in construct and in other methods involved in object creation (set_state, unserialize, | ||
+ | |||
+ | Unless otherwise specified in visibility, the write/unset access is available within protected scope. | ||
+ | |||
+ | |||
+ | == Reflection == | ||
+ | |||
+ | When using reflection, methods such as '' | ||
+ | |||
+ | Furthermore the following method is proposed added to be able to detect immutable properties: | ||
+ | - '' | ||
+ | |||
+ | |||
+ | " | ||
+ | |||
+ | //TODO: Expand this with specific modifier int for " | ||
+ | |||
+ | ==== 2. Attributes ==== | ||
+ | |||
+ | With the recently accepted [[rfc: | ||
+ | |||
+ | However the Attribute RFC does not allow for what is being drafted here, so this would need suggesting a way for userland classes to tell parser / compiler to enhance language features. | ||
+ | |||
+ | **As such, maybe this should be completely omitted from the proposal?** Should we aim for concepts in 1.1, 1.2 and 3.0 in this RFC? | ||
+ | |||
+ | |||
+ | === 2.1 Readonly attribute === | ||
+ | |||
+ | |||
+ | This proposal adds support for runtime-enforced readonly write visibility for declared properties. The following example illustrates the basic syntax: | ||
+ | |||
+ | <code php> | ||
+ | use PHP\Attribute\PropertyVisibility\Readonly; | ||
+ | |||
+ | class User { | ||
+ | << | ||
+ | public int $id; | ||
+ | |||
+ | // This property is not readonly | ||
+ | public string $email; | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Attribute can also be set on class level, implicit setting it on all fields unless they have their own PropertyVisibility attribute: | ||
+ | |||
+ | <code php> | ||
+ | use PHP\Attribute\PropertyVisibility\Readonly; | ||
+ | |||
+ | << | ||
+ | class User { | ||
+ | // This property is readable in public scope and writeable in protected | ||
+ | public int $id; | ||
+ | | ||
+ | // This property is readable in protected scope and writeable in private | ||
+ | protected string $email; | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | //For readonly semantics see proposal 1.2// | ||
+ | |||
+ | |||
+ | == Reflection == | ||
+ | |||
+ | //TODO: show example on reading /setting attribute via reflection, and how this relates to " | ||
+ | |||
+ | |||
+ | |||
+ | === 2.2. Immutable attribute === | ||
+ | |||
+ | This proposal adds a compiler attribute which implies a runtime-enforced immutable write visibility checks for declared properties. The following example illustrates the basic syntax: | ||
+ | |||
+ | <code php> | ||
+ | use PHP\Attribute\PropertyVisibility\Immutable; | ||
+ | |||
+ | class User { | ||
+ | << | ||
+ | public int $id; | ||
+ | |||
+ | public string $email; | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | Attribute can also be set on class level, implicit setting it on all fields unless they have their own PropertyVisibility attribute: | ||
+ | |||
+ | <code php> | ||
+ | use PHP\Attribute\PropertyVisibility\Immutable; | ||
+ | |||
+ | << | ||
+ | class User { | ||
+ | public int $id; | ||
+ | public string $email; | ||
+ | |||
+ | public function __construct(int $id, string $email) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | //For immutable semantics see proposal 1.3// | ||
+ | |||
+ | == Reflection == | ||
+ | |||
+ | //TODO: show example on reading /setting attribute via reflection, and how this relates to '' | ||
Line 119: | Line 413: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | Code that expects to be able to make properties writeable via reflection | + | Code that expects to be able to make properties writeable via reflection will have to adapt for new code taking advantage of this. |
+ | |||
+ | While '' | ||
Line 132: | Line 428: | ||
===== Performance ===== | ===== Performance ===== | ||
- | // | + | // |
===== Vote ===== | ===== Vote ===== | ||
As this is a language change, a 2/3 majority is required. | As this is a language change, a 2/3 majority is required. | ||
- | //RFC is in draft, and will undergo discussion phase before it is put for a vote.// | + | |
+ | ===== References ===== | ||
+ | |||
+ | |||
+ | * [[https://docs.microsoft.com/ | ||
+ | * [[https:// | ||
Line 150: | Line 451: | ||
Significant changes to the RFC are noted here. | Significant changes to the RFC are noted here. | ||
- | * 2020-06-xx (...) | + | * 2020-06-21 Initial |
- | * 2020-06-20 | + | |
rfc/readonly_and_immutable_properties.1592672733.txt.gz · Last modified: 2020/06/20 17:05 by andrerom