rfc:property_write_visibility

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:property_write_visibility [2020/06/28 19:17] – Simplify reflection and add link to Swift syntax for discussion andreromrfc:property_write_visibility [2020/07/07 07:33] (current) andrerom
Line 1: Line 1:
-====== PHP RFC: Property write visibility ====== +====== PHP RFC: Property write/set visibility ====== 
-  * Version: 0.4 +  * Version: 0.4.6 
-  * Date: 2020-06-25+  * Date: 2020-06-29
   * Author: André Rømcke <andre.romcke+php@gmail.com>   * Author: André Rømcke <andre.romcke+php@gmail.com>
-  * Proposed PHP version: PHP 8.0 +  * Proposed PHP version: PHP 8.1 
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/readonly_and_immutable_properties   * First Published at: http://wiki.php.net/rfc/readonly_and_immutable_properties
-  * Discussion: https://externals.io/message/ //coming//+  * Discussion: https://externals.io/message/110768
  
 ===== Introduction ===== ===== Introduction =====
  
-With the introduction of [[rfc:typed_properties_v2|typed properties]] in PHP 7.4, properties have become far more powerful. However there are still some common scenarios where you'll need to use magic methods for properties. Namely disconnected write vs read visibility for properties, like readonly, writeonly or immutable //like// semantic. This requires unnecessary boilerplate, makes usage less ergonomic and hurts performance.+With the introduction of [[rfc:typed_properties_v2|typed properties]] in PHP 7.4, properties have become far more powerful. However there are still common scenarios where you'll need to use magic methods or methods for properties in order to deal with disconnected write/set vs read/get visibility for properties, like readonly, writeonly or immutable //like// semantic. This requires unnecessary boilerplate, makes usage less ergonomic and in the case of magic methods hurts performance.
  
-This RFC resolves this issue by proposing to allow classes to optionally declare property write visibility, disconnected from read visibility.+This RFC resolves this issue by proposing to allow classes to optionally declare property write/set visibility, disconnected from read/get visibility.
  
  
-Under this RFC, code like+Under this RFC, code like the following using magic methods:
  
 <code php> <code php>
 +/**
 + * @property-read int $id
 + * @property-read string $name
 + */
 class User { class User {
     private int $id;     private int $id;
Line 57: Line 61:
 </code> </code>
  
-might be written as+might be written as //(Language syntax Option A)//:
  
 <code php> <code php>
Line 70: Line 74:
 } }
 </code> </code>
 +
 +
 +or //(Language Syntax Option B)//:
 +
 +<code php>
 +class User {
 +    public private(set) int $id;
 +    public protected(set) string $name;
 +
 +    public function __construct(int $id, string $name) {
 +        $this->id = $id;
 +        $this->name = $name;
 +    }
 +}
 +</code>
 +
  
  
Line 75: Line 95:
 ===== Main differences to previous proposals ===== ===== Main differences to previous proposals =====
  
-This RFC is inspired by past RFCs and the discussions that happened around them. Especially credit to [[rfc:readonly_properties|Readonly properties]] by Andrea Faulds, and discussions that happened around the different flavors of [[rfc:propertygetsetsyntax-v1.2|Property Accessors Syntax]].+This RFC is inspired by what was proposed on internals mailing list in "RFC Proposal - Attributes read/write visibility" by Amaury Bouchardon 15 July 2012. And the syntax and semantics found in [[https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18|Swift]]
 + 
 +In both cases the purpose is to provide for a wider set of use cases.
  
 ==== Readonly ==== ==== Readonly ====
  
-This RFC allows for among others semantics proposed in [[rfc:readonly_properties|Readonly properties]] (2014, Withdrawn), by using ''public:protected''.+This RFC allows for among others semantics proposed in [[rfc:readonly_properties|Readonly properties]] (2014, Withdrawn), by setting read/get as ''public'' and write/set as ''protected''.
  
 +This RFC does however **not** introduce any native readonly keyword/attribute which would be more readable, however this provides the underlying language concepts needed for introducing a Readonly attribute later.
  
 ==== Immutability ==== ==== Immutability ====
  
-This RFC allows for use cases **similar** to what was proposed in [[rfc:immutability|Immutability]] (2018, Stale), by using for instance ''public:private''.+This RFC allows to //simulate// what was proposed in [[rfc:immutability|Immutability]] (2018, Stale), by setting read/get as ''public'' and write/set as ''private''. 
 + 
 +This RFC does however **not** introduce any immutable knowhow in the language which JIT can optimize for, however the features here can be built upon for a native immutable keyword/attribute in the future. 
 + 
 + 
 +==== Write once ====
  
 This RFC does __not__ align with the semantics of the recent [[rfc:write_once_properties|Write once properties]] (2020, Declined), which is targeting a different problem. This RFC does __not__ align with the semantics of the recent [[rfc:write_once_properties|Write once properties]] (2020, Declined), which is targeting a different problem.
Line 93: Line 121:
 This RFC does not try to solve as wide use case as the different iterations of [[rfc:propertygetsetsyntax-v1.2|Property Accessors Syntax]] (2012, Declined) does. This RFC does not try to solve as wide use case as the different iterations of [[rfc:propertygetsetsyntax-v1.2|Property Accessors Syntax]] (2012, Declined) does.
  
-However with what being proposed here and [[rfc:typed_properties_v2|typed properties]], most common use cases needed for properties are covered in an arguably simpler way.+However what being proposed here is aligned to make sure Accessors can cleanly be added later.
  
  
 ===== Proposal ===== ===== Proposal =====
  
-This proposal adds support for enforced write visibility checks for declared properties. The following example illustrates the basic syntax:+This proposal adds support for enforced write/set visibility checks for declared properties.  
 + 
 +==== Language syntax A: "public:private" ==== 
 + 
 +The following example illustrates the basic syntax:
  
 <code php> <code php>
Line 109: Line 141:
          
     // Property is write-only in public and protected scope     // Property is write-only in public and protected scope
-    private:public string $newName;+    private:public string $newPassword;
  
     public function __construct(int $id, string $name) {     public function __construct(int $id, string $name) {
Line 118: Line 150:
 </code> </code>
  
-The format is ''<read_visibility>:<write_visibility>'', and if you omit the last visibility value you will like before implicit set both read and write visibility at once.+The format is ''<get_visibility>:<set_visibility>'', and if you omit the last visibility value you will like before implicit set both read and write visibility at once.
  
-==== References ====+==== Language syntax B: "private(set)" ====
  
 +The following example illustrates the basic syntax:
 +
 +<code php>
 +class User {
 +    // Property is readonly in protected and public scope
 +    public private(set) int $id;
 +
 +    // Property is readonly in public scope
 +    public protected(set) string $name;
 +    
 +    // Property is write-only in public and protected scope
 +    private public(set) string $newPassword;
 +
 +    public function __construct(int $id, string $name) {
 +        $this->id = $id;
 +        $this->name = $name;
 +    }
 +}
 +</code>
 +
 +The format in this option is taken from [[https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18|Swift]], and is perhaps more readable in terms of intent. It also aligns nicely with vocabulary of future Accessors proposal.
 +
 +Like in the other syntax proposal, if set visibility is not specified, as before the global visibility will define both read/get and write/set visibility.
 +
 +==== References ====
  
 Attempting to pass a property value outside of allowed writable scope as a reference, results in an error. Attempting to pass a property value outside of allowed writable scope as a reference, results in an error.
Line 128: Line 185:
 ==== Reflection ==== ==== Reflection ====
  
-When using reflection, methods such as ''ReflectionProperty::setAccessible()'' will work as before, it will implicit set visibility for both read and write.+When using reflection, methods such as ''ReflectionProperty::setAccessible()'' will work as before, it will implicit set visibility for both read/get and write/set.
  
 In order to avoid backwards compatibility issue, the following methods will get updated behavior: In order to avoid backwards compatibility issue, the following methods will get updated behavior:
  
-''ReflectionProperty::isPrivate()'' — Checks if property is private for read and write visibility, or one of them +  * ''ReflectionProperty::isPrivate()'' Checks if property is private **for get and set visibility, or one of them** 
-''ReflectionProperty::isProtected()'' - Checks if property is protected for read and write visibility, or one of them with the remaining being public +  ''ReflectionProperty::isProtected()'' - Checks if property is protected **for get and set visibility, or one of them with the remaining being public** 
-''ReflectionProperty::isPublic()'' - Checks if property is public for read and write visibility+  ''ReflectionProperty::isPublic()'' - Checks if property is public **for get and set visibility**
  
 The following methods needs to be added to detect different read vs write visibility:  The following methods needs to be added to detect different read vs write visibility: 
  
-''ReflectionProperty::isWritePrivate()'' — Checks if property is writable in private +  * ''ReflectionProperty::isSetPrivate()'' Checks if property is writable in private 
-''ReflectionProperty::isWriteProtected()'' — Checks if property is writable in protected +  ''ReflectionProperty::isSetProtected()'' Checks if property is writable in protected 
-''ReflectionProperty::isWritePublic()'' — Checks if property is writable in public+  ''ReflectionProperty::isSetPublic()'' Checks if property is writable in public
  
-''ReflectionProperty::isReadPrivate()'' — Checks if property is readable in private +  * ''ReflectionProperty::isGetPrivate()'' Checks if property is readable in private 
-''ReflectionProperty::isReadProtected()'' — Checks if property is readable in protected +  ''ReflectionProperty::isGetProtected()'' Checks if property is readable in protected 
-''ReflectionProperty::isReadPublic()'' — Checks if property is readable in public+  ''ReflectionProperty::isGetPublic()'' Checks if property is readable in public
  
  
Line 155: Line 212:
 ==== Language Syntax ==== ==== Language Syntax ====
  
-The format being proposed here ''<read_visibility>:<write_visibility>'' is open for discussion if for instance this causes problems for parseror is deemed otherwise in-practical.+The format options being proposed here are open for discussion, and additional proposal can be made on internals list.
  
-If we where to just have two separate visibility keywords on properties, where second one will denote write visibility, that can cause issues for API's such as ''Reflection::getModifierNames()'', and arguably lowers readability.+Plain "public private $var" was on purpose skipped as it is less readable and could easily cause issues for ''Reflection::getModifierNames()''.
  
-One other alternative would be to align withor take inspiration from [[https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18||Swift setter access level syntax]].+==== Why not a Readonly keyword/attribute ==== 
 + 
 +Several comments are pointing out that ''readonly'', ''writeonly'', ''immutable'' keywords/attributes would be more readableand this is trueHowever what is proposed here is made in such a way that for instance a ''Readonly'' attribute can be introduced more easily in the future. 
 + 
 +AS in, if the language don't have a concept for write/set property visibility, then we'll end up with having to introduce reflection api that are tied in to the keyword/attribute introduced instead, as opposed to the underlying concept.
  
  
Line 169: Line 230:
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
  
-Next PHP version, 8.suggested.+Next PHP version, 8.suggested.
  
  
Line 191: Line 252:
 ===== References ===== ===== References =====
  
-  * [[https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18||Swift setter access level]]+  * [[https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID18|Swift setter access level]]
   * [[https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#readonly-fields|C# readonly fields]], semantically similar to what is referred to as "immutable" here.   * [[https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#readonly-fields|C# readonly fields]], semantically similar to what is referred to as "immutable" here.
   * [[https://docs.rs/readonly/0.1.6/readonly/|Rust readonly create]]   * [[https://docs.rs/readonly/0.1.6/readonly/|Rust readonly create]]
Line 205: Line 266:
 Significant changes to the RFC are noted here. Significant changes to the RFC are noted here.
  
 +
 +  * 2020-06-29 Adapt for initial feedback, add syntax proposal aligned with Swift
   * 2020-06-28 Simplify Reflection API proposal, add syntax alternatives for discussion   * 2020-06-28 Simplify Reflection API proposal, add syntax alternatives for discussion
   * 2020-06-25 Focus on write visibility proposal   * 2020-06-25 Focus on write visibility proposal
   * 2020-06-20 Initial early draft to get feedback on direction between visibility, readonly/immutable keywords or attributes   * 2020-06-20 Initial early draft to get feedback on direction between visibility, readonly/immutable keywords or attributes
rfc/property_write_visibility.txt · Last modified: 2020/07/07 07:33 by andrerom