Re: [RFC] [DISCUSSION] Immutable/final/readonly properties
| From: | Marco Pivetta | Date: | Mon, 24 Feb 2020 03:26:19 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Immutable/final/readonly properties | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108733@lists.php.net to get a copy of this message | ||
Hey Larry,
On Sun, Feb 23, 2020, 18:06 Larry Garfield <larry@garfieldtech.com> wrote:
>
> It does not.
>
> 1) Race condition if I assume that a public write-once property is a
> materialized value, but access it before it gets materialized.
>
This is alrey true for any public typed properties without default values,
and why static analysis tools pick up missing initialisation since ages.
> 2) Race condition if internal non-constructor code wants to set the value,
> but some external routine has set it first.
>
Internal ctors are part of the category above: tools like psalm pick this
up as well.
> 3) Cloning creates an interesting and complicated case of both of the
> above. Does a cloned object start with its write-once bits reset or no?
> There's problems both ways.
>
Not a problem: we seem to be making a problem out of the pattern used in
current PSR-7 implementations, which is following:
final class Foo
{
public function withBar($bar):self {
$instance = clone $this;
$instance->foo = $foo;
return $instance;
}
}
The solution is trivial: don't use cloning:
final class Foo
{
public function withBar($bar):self {
$instance = new self();
$instance->foo = $foo;
// more assignments here - unavoidable
return $instance;
}
}