rfc:readonly_and_immutable_properties
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
rfc:readonly_and_immutable_properties [2020/06/21 12:47] – some further fixes andrerom | rfc:readonly_and_immutable_properties [2020/06/21 18:47] (current) – formating andrerom | ||
---|---|---|---|
Line 14: | Line 14: | ||
**This is a early draft, currently looking for feedback on direction on what would make most sense to propose, especially if there is any point in even exploring using Attributes for the features covered here or not.** | **This is a early draft, currently looking for feedback on direction on what would make most sense to propose, especially if there is any point in even exploring using Attributes for the features covered here or not.** | ||
- | 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), for immutable semantic it's even more cumbersome. This requires unnecessary boilerplate, | + | 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, |
This RFC resolves this issue by proposing a few options: | This RFC resolves this issue by proposing a few options: | ||
Line 22: | Line 22: | ||
- immutable 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: | - Attribute approach: | ||
- | - Readonly attribute for properties, if #1.1 is accepted this is merely syntax sugar that adds possibility to set readonly on classes (implicit all properties). | + | - Readonly attribute for properties, if #1.1/1.2 is accepted this is merely syntax sugar. |
- | - Immutable attribute for properties, if #1.2 is accepted this is merely syntax sugar that adds possibility to set immutability on classes (implicit all properties). | + | - Immutable attribute for properties, if #1.3 is accepted this is merely syntax sugar |
- | + | ||
- | + | ||
- | //One of the options here are intended to be ruled out during early discussions// | + | |
Line 111: | Line 108: | ||
==== Readonly ==== | ==== Readonly ==== | ||
- | This RFC aligns with [[rfc: | + | This RFC aligns with [[rfc: |
Line 117: | Line 114: | ||
- | This RFC aligns with [[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 125: | 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 readonly, and does not offer solutions to immutability | |
- | Thus the author of this RFC believes what is proposed here should be done before any proposal | + | - There seems to be a higher need in the community |
+ | - Everything Accessors offers beyond disconnected read and write visibility | ||
Line 156: | Line 154: | ||
public: | public: | ||
| | ||
- | // Property is writeonly | + | // Property is write-only |
private: | private: | ||
Line 171: | Line 169: | ||
== Reflection == | == Reflection == | ||
- | When using reflection, methods such as "ReflectionProperty:: | + | When using reflection, methods such as '' |
However with this proposal the following existing methods will represent read visibility for cases where it differs: | However with this proposal the following existing methods will represent read visibility for cases where it differs: | ||
- | - ReflectionProperty:: | + | - '' |
- | - ReflectionProperty:: | + | - '' |
- | - ReflectionProperty:: | + | - '' |
And for checking separate write visibility the following methods may be used: | And for checking separate write visibility the following methods may be used: | ||
- | - ReflectionProperty:: | + | - '' |
- | - ReflectionProperty:: | + | - '' |
- | - ReflectionProperty:: | + | - '' |
- | "Reflection:: | + | '' |
//TODO: Expand this with modifier ints representing all variations and their names// | //TODO: Expand this with modifier ints representing all variations and their names// | ||
Line 238: | Line 236: | ||
== Reflection == | == Reflection == | ||
- | When using reflection, methods such as "ReflectionProperty:: | + | When using reflection, methods such as '' |
- | Furthermore the following method is proposed added to be able to detect | + | Furthermore the following method is proposed added to be able to detect |
- | - ReflectionProperty:: | + | - '' |
- | "Reflection:: | + | '' |
- | //TODO: Expand this with specific modifier int for "immutable"// | + | //TODO: Expand this with specific modifier int for "readonly"// |
Line 295: | Line 293: | ||
== Reflection == | == Reflection == | ||
- | When using reflection, methods such as "ReflectionProperty:: | + | When using reflection, methods such as '' |
Furthermore the following method is proposed added to be able to detect immutable properties: | Furthermore the following method is proposed added to be able to detect immutable properties: | ||
- | - ReflectionProperty:: | + | - '' |
Line 307: | Line 305: | ||
==== 2. Attributes ==== | ==== 2. Attributes ==== | ||
- | With the recently accepted [[rfc: | + | 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. | 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. | ||
Line 409: | Line 407: | ||
== Reflection == | == Reflection == | ||
- | //TODO: show example on reading /setting attribute via reflection, and how this relates to "ReflectionProperty:: | + | //TODO: show example on reading /setting attribute via reflection, and how this relates to '' |
Line 417: | Line 415: | ||
Code that expects to be able to make properties writeable via reflection will have to adapt for new code taking advantage of this. | Code that expects to be able to make properties writeable via reflection will have to adapt for new code taking advantage of this. | ||
- | While ReflectionProperty:: | + | While '' |
Line 430: | 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 ===== | ===== References ===== | ||
Line 441: | Line 438: | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
rfc/readonly_and_immutable_properties.1592743650.txt.gz · Last modified: 2020/06/21 12:47 by andrerom