rfc:readonly_properties

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_properties [2014/10/23 23:33] – '''' ajfrfc:readonly_properties [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: Readonly Properties ====== ====== PHP RFC: Readonly Properties ======
-  * Version: 0.1 +  * Version: 0.1.1 
-  * Date: 2014-10-24+  * Date: 2014-10-24 (Withdrawn 2014-11-03)
   * Author: Andrea Faulds, ajf@ajf.me   * Author: Andrea Faulds, ajf@ajf.me
-  * Status: Under Dicussion+  * Status: Withdrawn 
 +  * Discussion: http://markmail.org/message/7l3ci3sboma2nlzq
   * First Published at: http://wiki.php.net/rfc/readonly_properties   * First Published at: http://wiki.php.net/rfc/readonly_properties
  
 ===== Introduction ===== ===== Introduction =====
  
-There is currently no way to make a property readable to everyone and writeable only to the containing object, with PHP's visibility specifiers allowing all of nothing: a scope can either read or write, or do neither. While ''_''''_get'' and ''_''''_set'' exist, these do not actually allow control of the same property, merely only exposing a separate property, and they are only usable for undeclared properties, which cannot be reflected and are not performant.+There is currently no way to make a property readable to everyone and writeable only to the containing object, with PHP's visibility specifiers allowing all or nothing: a scope can either both read and write, or do neither. While ''_''''_get'' and ''_''''_set'' exist, these do not actually allow control of the same property, merely only exposing a separate property, and they are only usable for undeclared properties, which cannot be reflected and are not performant.
  
 Because of this, getter and setter functions to control the ability to write to properties are common. This requires developers to write boilerplate code, even if assisted by IDEs, and is not as performant as a raw property. Because of this, getter and setter functions to control the ability to write to properties are common. This requires developers to write boilerplate code, even if assisted by IDEs, and is not as performant as a raw property.
Line 18: Line 19:
 Due to implementation difficulties, this doesn't support for static properties for now. This may yet change, however. Properties cannot be both ''private'' and ''readonly'', since there is no more restrictive scope than ''private''. Due to implementation difficulties, this doesn't support for static properties for now. This may yet change, however. Properties cannot be both ''private'' and ''readonly'', since there is no more restrictive scope than ''private''.
  
-This keyword's behaviour should not be confused with that of C#'s ''readonly'' specifier, which makes a property writeable only once, from a class's constructor. While the possible confusion this could produce is unfortunate, I could not think of a better name after much discussion. I don't think this precludes inclusion of a feature similar to that of C# under another name as there are other possible names for that feature, such as ''immutable'' or ''final''.+This keyword's behaviour should not be confused with that of C#'s ''readonly'' specifier, which makes a property writeable only once, from a class's constructor. While the possible confusion this could produce is unfortunate, I could not think of a better name after much discussion. I don't think this precludes inclusion of a feature similar to that of C# under another name as there are other possible names for that feature, such as ''immutable'' or ''final''. It's also worth noting that ''readonly'' is already used for some classes (such as the DOM) in the PHP manual.
  
 ==== Example ==== ==== Example ====
Line 28: Line 29:
     private $elements = [];     private $elements = [];
     public function push($elem) {     public function push($elem) {
-        $this->length++;+        $this->size++;
         $this->elements[] = $elem;         $this->elements[] = $elem;
     }     }
     public function pop() {     public function pop() {
-        $this->length--;+        $this->size--;
         return array_pop($this->elements);         return array_pop($this->elements);
     }     }
Line 54: Line 55:
 This is proposed for the next major version of PHP, currently PHP 7. This is proposed for the next major version of PHP, currently PHP 7.
  
 +===== Future Scope =====
 +
 +Properties in interfaces are currently not supported. If they were later to be supported (perhaps with getters/setters being added), ''readonly'' could be used to avoid compelling implementing classes from making a property writeable, but not preventing them from expanding the interface to do so, e.g.:
 +
 +<code php>
 +interface Point {
 +    readonly $x, $y;
 +}
 +
 +class ImmutablePoint implements Point {
 +    public readonly $x, $y;
 +    ...
 +}
 +
 +class MutablePoint implements Point {
 +    public $x, $y;
 +    ...
 +}
 +</code>
  
 ===== RFC Impact ===== ===== RFC Impact =====
Line 97: Line 117:
  
 ===== Changelog ===== ===== Changelog =====
 +   
 +  * v0.1.1 - Added Future Scope
   * v0.1 - Creatd   * v0.1 - Creatd
rfc/readonly_properties.1414107189.txt.gz · Last modified: 2017/09/22 13:28 (external edit)