Both sides previous revision
Previous revision
Next revision
|
Previous revision
|
rfc:write_once_properties [2020/03/16 11:47] kocsismate Disallow defining default values |
rfc:write_once_properties [2020/03/31 07:51] (current) kocsismate Closed the vote |
* Author: Máté Kocsis <kocsismate@php.net> | * Author: Máté Kocsis <kocsismate@php.net> |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 |
* Status: Under Discussion | * Status: Declined |
* Implementation: https://github.com/php/php-src/pull/5186 | * Implementation: https://github.com/php/php-src/pull/5186 |
| |
$this->b; = &$var; // EXCEPTION: reference isn't allowed | $this->b; = &$var; // EXCEPTION: reference isn't allowed |
| |
key($foo->b); // SUCCESS: internal pointer of arrays is possible to read | key($foo->b); // SUCCESS: internal pointer of arrays is possible to read |
$foo->c = new Foo(); // SUCCESS: property d hasn't been initialized before | $foo->c = new stdClass(); // SUCCESS: property c hasn't been initialized before |
$foo->d->foo = "foo"; // SUCCESS: objects are still mutable internally | $foo->c->foo = "foo"; // SUCCESS: objects are still mutable internally |
| |
</code> | </code> |
| |
class Foo { | class Foo { |
public int $a = 0; | <keyword> public int $a = 0; |
} | } |
| |
| |
===== Future Scope ===== | ===== 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. I also plan to address the problem with cloning mentioned in the proposal section. | 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. I also plan to address the problem with cloning mentioned in the "Proposal" section. |
| |
Additionally, as mentioned in the "Compile-Type Restrictions" section, adding support for the ''mixed'' type would make it possible to use "write-once" properties together with resources. Besides this, we could allow the definition of a default values later on as soon as we have a good use-case for them. | Additionally, as mentioned in the "Compile-Type Restrictions" section, adding support for the ''mixed'' type would make it possible to use "write-once" properties together with resources. Besides this, we could allow the definition of default values later on as soon as we have a good use-case for them. |
| |
Finally, "write-once" properties could in principle support covariance. That is, a subclass would be allowed to tighten the property type that is inherited from the parent class, while other properties must stay invariant. All this would be possible because of the quasi-immutable nature of "write-once" properties: they are generally expected to be assigned to only once, in the constructor - which is exempt from LSP checks. There is a gotcha though: in practice, "write-once" properties could be written from places other than the constructor. Although there might not be many practical use-cases for it, the infamous setter injection is certainly one (as shown at https://3v4l.org/DQ3To), in which case property covariance would be a problem. | Finally, "write-once" properties could in principle support covariance. That is, a subclass would be allowed to tighten the property type that is inherited from the parent class, while other properties must stay invariant. All this would be possible because of the quasi-immutable nature of "write-once" properties: they are generally expected to be assigned to only once, in the constructor - which is exempt from LSP checks. There is a gotcha though: in practice, "write-once" properties could be written from places other than the constructor. Although there might not be many practical use-cases for it, the infamous setter injection is certainly one (as shown at https://3v4l.org/DQ3To), in which case property covariance would be a problem. |
| |
===== Proposed Voting Choices ===== | ===== Vote ===== |
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. | |
| The vote starts on 2020-03-17 and ends on 2020-03-31. The primary vote requires 2/3, while the secondary one requires a simple majority to be accepted. |
| |
| <doodle title="Do you want to add support for write-once properties?" auth="kocsismate" voteType="single" closed="true"> |
| * Yes |
| * No |
| </doodle> |
| |
| ---- |
| |
| <doodle title="Which keyword to use?" auth="kocsismate" voteType="single" closed="true"> |
| * immutable |
| * locked |
| * writeonce |
| * readonly |
| </doodle> |
| |
===== References ===== | ===== References ===== |
Prior RFC proposing ''immutable'' properties: https://wiki.php.net/rfc/immutability | Prior RFC proposing ''immutable'' properties: https://wiki.php.net/rfc/immutability |
| |