====== PHP RFC: Write-Once Properties ====== * Version: 0.1 * Date: 2020-02-18 * Author: Máté Kocsis, * Target Version: PHP 8.0 * Status: Draft * Implementation: https://github.com/php/php-src/pull/5186 ===== Introduction ===== This RFC proposes to add support for a new property modifier that would allow properties to be initialized, but not modified afterwards. This feature would be useful in situations where one wants to guarantee that a property remains the same for the lifetime of an object - which is usually the case for Value Objects or Data Transfer Objects. Other languages, like Java and C# also have similar - but not exactly the same - concepts for a long time (''final'' and ''readonly'' respectively). ===== Proposal ===== "Write-once" properties in PHP (the actual keyword is to be decided) could be initialized either by an explicit default value, or by assigning a value to them. Unlike to ''final'' properties in Java, this RFC proposes to allow the initialization of object properties after object construction. The main purpose of this approach is to make lazy loading possible - which is an important aspect for many PHP applications. In addition to object properties, class properties can also use the modifier in question with the same rules. As soon as initialization is done, any other attempt to assign a value to "write-once" properties results in an exception. Besides assignment, the increment, decrement, and unset operations are also forbidden. As arrays are an immutable data structure in PHP, any attempt to mutate a property of array type (adding/removing/changing items) is forbidden. However, properties of object or resource types still remain mutable internally. In order to avoid problems with references, references on "write-once" properties are disabled as well. class Foo { public int $a = 1; public string $b; public array $c = ["foo"]; public object $d; public function __construct() { $this->b = "foo"; } } $foo = new Foo(); $foo->a = 2; // EXCEPTION: property a has already been initialized $foo->b = "bar"; // EXCEPTION: property b has already been initialized $foo->a++; // EXCEPTION: incrementing/decrementing is forbidden unset($foo->c); // EXCEPTION: unsetting is forbidden $foo->c[] = "bar"; // EXCEPTION: arrays can't be modified $foo->d = new Foo(); // SUCCESS: property d hasn't been initialized before $foo->d->foo = "foo"; // SUCCESS: objects are treated by-reference As untyped properties have an implicit default value (''null'') in the absense of an explicit one, their usefulness would be very limited. In order to avoid the introduction or unintiutive workarounds, this RFC proposes to disable the property modifier in question for them. Contrarily to untyped properties, typed properties are in uninitialized state by default, so they play well with the write-once semantics. ===== Discussion ===== ==== Approach ==== ==== Name choice ==== As there is no consensus about the name of the modifier, I'd like to put it to vote. You can find below the ideas that came up during discussion along with some pros/cons: * ''final'': It would be a better fit for a modifier that prevents a property to be overridden (just like how ''final'' works in case of classes and methods) * ''immutable'': Actually, this name is a lie since the usage of mutable data structures are not restricted (objects, resources) at all * ''readonly'': Although it' a familiar term from C#, this name also lies as the proposed modifier actually works according to "write-once" semantics * ''writeonce'': It's the most clear and most descriptive name among all, but it doesn't sound familiar at all * ''locked'': It's a more abstract, less clear name for the feature, but at least it doesn't lie * ''sealed'': It has the same properties as ''locked'', and this keyword is also used in C# (similarly to ''final'' in PHP) Considering the above, "locked", "sealed", and "writeonce" is going to be proposed as voting choices of the decision about the keyword. ===== Backward Incompatible Changes ===== There are no backward incompatible changes in this proposal except for the fact that "locked", "sealed", or "writeonce" would become a reserved keyword depending on the outcome of the secondary vote. ===== Future Scope ===== Adding support for "write-once" properties would lay the groundwork for immutable objects - for which I'm going to create a proposal should the current RFC be accepted. ===== Proposed Voting Choices ===== The primary vote ("Do you want to add support for the proposed property modifier?") requires 2/3 majority, while the secondary one ("Which keyword to use"?) requires a simple majority. ===== References ===== Prior RFC proposing the introduction of ''immutable'' properties: https://wiki.php.net/rfc/immutability