rfc:readonly_properties
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:readonly_properties [2014/10/23 23:27] – created ajf | rfc: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 |
* Author: Andrea Faulds, ajf@ajf.me | * Author: Andrea Faulds, ajf@ajf.me | ||
- | * Status: | + | * Status: |
+ | * Discussion: http:// | ||
* First Published at: http:// | * First Published at: http:// | ||
===== 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 '' | + | 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 |
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, | Due to implementation difficulties, | ||
- | This keyword' | + | This keyword' |
==== Example ==== | ==== Example ==== | ||
Line 28: | Line 29: | ||
private $elements = []; | private $elements = []; | ||
public function push($elem) { | public function push($elem) { | ||
- | $this->length++; | + | $this->size++; |
$this-> | $this-> | ||
} | } | ||
public function pop() { | public function pop() { | ||
- | $this->length--; | + | $this->size--; |
return array_pop($this-> | return array_pop($this-> | ||
} | } | ||
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/ | ||
+ | |||
+ | <code php> | ||
+ | interface Point { | ||
+ | readonly $x, $y; | ||
+ | } | ||
+ | |||
+ | class ImmutablePoint implements Point { | ||
+ | public readonly $x, $y; | ||
+ | ... | ||
+ | } | ||
+ | |||
+ | class MutablePoint implements Point { | ||
+ | public $x, $y; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 91: | Line 111: | ||
* An identical proposal was made eight years ago to internals as part of this discussion: http:// | * An identical proposal was made eight years ago to internals as part of this discussion: http:// | ||
+ | * This provides some of the functionality 2013's rejected [[rfc: | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Keep this updated with features that were discussed on the mail lists. | Keep this updated with features that were discussed on the mail lists. | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | | ||
+ | * v0.1.1 - Added Future Scope | ||
+ | * v0.1 - Creatd |
rfc/readonly_properties.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1