rfc:immutability
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:immutability [2016/12/02 14:26] – marijic.silvio | rfc:immutability [2018/02/19 21:13] – marijic.silvio | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Immutable classes and properties ====== | ====== PHP RFC: Immutable classes and properties ====== | ||
- | * Version: 0.9 | + | * Version: 0.2 |
- | * Date: 2016-08-12 | + | * Date: 2018-02-19 |
* Author: Michal Brzuchalski < | * Author: Michal Brzuchalski < | ||
* Author: Silvio Marijic < | * Author: Silvio Marijic < | ||
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC proposes introduction of immutable classes and properties. Currently the only way of achieving immutability is trough | + | This RFC proposes |
- | If those mechanizsm | + | If this mechanism |
Line 18: | Line 18: | ||
- Safe for concurrency. | - Safe for concurrency. | ||
- Value objects, DTO's etc. can be easily created. | - Value objects, DTO's etc. can be easily created. | ||
- | - Properties can be public which removes need for getters without allowing state modification. | + | - Properties can be public which removes |
- (Please point it out more advantages) | - (Please point it out more advantages) | ||
**Cons** | **Cons** | ||
- | - (Please point it out more disadvantages) | + | - Currently arrays on immutable properties are not supported. |
Line 29: | Line 29: | ||
==== Immutable Class ==== | ==== Immutable Class ==== | ||
- | Class defined as immutable will imply immutability across all of it' | + | A class defined as immutable will imply immutability across all of its properties by default. After the object is constructed, it is not possible to modify |
<code php> | <code php> | ||
Line 46: | Line 46: | ||
</ | </ | ||
- | Changes to inheritance are made to add constraints when extending | + | Changes to inheritance are made to add constraints when extending |
<code php> | <code php> | ||
Line 56: | Line 56: | ||
==== Immutable Properties ==== | ==== Immutable Properties ==== | ||
- | Classes have ability to enforce immutability to only subset of properties if needed, in that case immutability will be implied only on properties that are delared | + | Classes have the ability to enforce immutability to only a subset of properties if needed, in that case, immutability will be implied only on properties that are declared as immutable. |
<code php> | <code php> | ||
Line 71: | Line 71: | ||
- | If immutable property contains object, to preserve immutability, | + | If an immutable property contains |
<code php> | <code php> | ||
immutable class Email{} | immutable class Email{} | ||
Line 84: | Line 84: | ||
</ | </ | ||
- | If immutable property of parent class is overridden in child class, it has to be declared as immutable. | + | Resources are not allowed to be assigned to immutable properties because of fact that resources by nature are not immutable. |
- | Since focus of this RFC is on immutable objects, having static properties in immutable classes will result in compile error. | + | <code php> |
+ | class File { | ||
+ | public immutable $handle; | ||
+ | |||
+ | public function __construct ($handle) { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $file = new File(fopen(' | ||
+ | </ | ||
+ | |||
+ | Arrays are not allowed to be assigned to immutable properties.. | ||
+ | <code php> | ||
+ | class A { | ||
+ | public immutable $x; | ||
+ | |||
+ | public function __construct ($x) { | ||
+ | $this->x = $x; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $a = new A([' | ||
+ | </ | ||
+ | |||
+ | If an immutable property of a parent class is overridden in a child class, it has to be declared as immutable. | ||
+ | Since the focus of this RFC is immutable objects, having static properties in immutable classes will result in a compile error. | ||
==== References ==== | ==== References ==== | ||
- | Assigning by references to immutable properties will result | + | Assigning by references to immutable properties will result |
<code php> | <code php> | ||
Line 108: | Line 134: | ||
</ | </ | ||
+ | ==== Comparison ==== | ||
+ | |||
+ | Identity of immutable object is based on its value. So two immutable are identical if they are of the same type and contain same value. | ||
+ | |||
+ | <code php> | ||
+ | immutable class Email { | ||
+ | public $email; | ||
+ | |||
+ | public function __construct ($email) { | ||
+ | // validation | ||
+ | |||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $email1 = new Email(" | ||
+ | $email2 = new Email(" | ||
+ | var_dump($email1 === $email2); // bool(true) | ||
+ | </ | ||
===== Examples ===== | ===== Examples ===== | ||
- | Notice in above examples removing | + | Notice in above example, changing |
- | Every example shows where internal object state is important. Any references to objects passed into immutable class constructor cannot be references to scalars or may be immutable class instances. That gives high guarantee you will keep internal state unchanged for all the time. | + | Every example shows where internal object state is important. Any references to objects passed into an immutable class constructor cannot be references to scalars or may be immutable class instances. That gives high guarantee you will keep internal state unchanged for all the time. |
---- | ---- | ||
Line 119: | Line 164: | ||
==== Money ==== | ==== Money ==== | ||
- | Money Pattern, defined by Martin Fowler and published in Patterns of Enterprise Application Architecture, | + | Money Pattern, defined by Martin Fowler and published in Patterns of Enterprise Application Architecture, |
<code php> | <code php> | ||
Line 253: | Line 298: | ||
</ | </ | ||
- | There is no need for getters because internally immutable object | + | There is no need for getters because |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | No backward | + | No backwardly |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | - PHP 7.2 | + | To be discussed. |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 294: | Line 339: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
+ | Add support for arrays on immutable properties. | ||
+ | Expand immutability to regular variables also. | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 301: | Line 348: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | [[https:// | + | [[https:// |
rfc/immutability.txt · Last modified: 2018/02/20 11:19 by marijic.silvio