Re: Non-nullable properties

From: Date: Sat, 14 Jul 2018 18:29:23 +0000
Subject: Re: Non-nullable properties
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-102825@lists.php.net to get a copy of this message
On Sat, Jul 14, 2018 at 10:00 AM Rowan Collins <rowan.collins@gmail.com> wrote: > > On 14 July 2018 15:37:32 BST, Levi Morrison <levim@php.net> wrote: > >> class Validity { > >> public \DateTimeInterface $validFrom; > >> public ?\DateTimeInterface $validTo = null; > >> } > > > >Even if this had a constructor we couldn't "trust" it because there > >are ways to skip constructors. > > Then the mechanisms should either be impossible with non-nullable properties, or have a way to > mark the object initialised. If not, the object created would not be fully initialised, so accessing > *the entire object* should be an error, not just accessing the specific properties which are marked > non-nullable. > > I'm not claiming any of this is easy, but if you can't guarantee a property has a > valid value, what is the type hint actually for? Whether it's a constructor, a factory method, or just a block of code near the new site: something has the responsibility for initialization. Once initialized the object provides guarantees unless you unset the property. It also provides the guarantee that if it is set then it will always be of the outlined type; this is a much stronger guarantee than we have today, which is... nothing. It's worth discussing, and if someone has a brilliant idea maybe we can proceed. However, in the absence of such an idea am I fine with the outlined semantics of the RFC.

« previous php.internals (#102825) next »