On Wed, Sep 19, 2018 at 11:17 PM Rowan Collins <rowan.coll...@gmail.com>
wrote:

> On 19/09/2018 21:04, Levi Morrison wrote:
> > I think this code should be allowed:
> >
> >     class User {
> >          public int $id;
> >          public string $preferred_name;
> >          public string $username;
> >      }
>
> Why? What contract is being enforced by that class that is not enforced
> by this class?
>
>     class User {
>          public ?int $id=null;
>          public ?string $preferred_name=null;
>          public ?string $username=null;
>      }
>
> Both require the consumer of the class to trust that someone, somewhere,
> has initialised the fields (and not subsequently unset them).
>
> Or have I misunderstood what you intended with that example?
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]
>

At least the approach without nullable properties will lead to a Throwable
when a read is attempted on an uninitialized object, which is still better
than nullability checks all over the place.

And yes, constructing an object without going through its constructor is
quite common anyway - this was indeed part of the upfront discussion, and
is also part of why I gave a +1 to the current RFC.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Reply via email to