Re: [RFC][DISCUSSION] - Immutable classes and properties
| From: | Larry Garfield | Date: | Mon, 12 Dec 2016 00:03:43 +0000 |
| Subject: | Re: [RFC][DISCUSSION] - Immutable classes and properties | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-97359@lists.php.net to get a copy of this message | ||
Assuming this was intended for the list...
On 12/11/2016 05:55 PM, Mathieu Rochette wrote:
Currently the only "unlocked context" for an object is its
constructor. As discussed previously, that is insufficient. For any
non-trivial object (more than 1-3 internal properties) creating a new
one via the constructor only when incrementally building is
prohibitively difficult. The pattern of with*() methods that spawn
new objects via clone(), a la PSR-7, needs to be supported. That is:
immutable class Money {
// ...
public function add(Money $other) : Money {
$new = clone($this);
$new->amount += $other->amount;
return $new;
}
}
I'm not sure how easily we can denote that sort of case. It's outside
the __clone() function itself, which is what makes it difficult.
Without that, though, such immutable objects are of only limited use.
As you said, it has been already been discussed that a method to build new altered object from an existing one could be improved. Different options were proposed but maybe it's better to start small to get this first part right and add this in another rfc ? having everything at the same time might makes the rfc more difficult to be acceptedOn the contrary, an RFC that doesn't fully solve the issue at hand and leaves major gaps in place is a poor RFC, and I would expect to be justifiably voted down. --Larry Garfield