Hey Larry,


On Sun, Feb 23, 2020, 18:06 Larry Garfield <la...@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;
    }
}

Reply via email to