On 18/05/2024 18:08, Rowan Tommins [IMSoP] <imsop....@rwec.co.uk> wrote:
> > In my opinion - and I stress that others may not share this opinion - > the entire concept of "uninitialized properties" is a wart on the > language, which we should be doing our best to eliminate, not adding > more features around it. > > As a bit of background, the concept was created when typed properties > were being added, to handle a limitation of the language: given the > declaration "public Foo $prop;" there is no way to specify an initial > value which meets the type constraint. For nullable properties, you can > write "public ?Foo $prop=null;" but since PHP (thankfully) distinguishes > nullable and non-nullable types, you can't write "public Foo $prop=null;" > > Some languages, e.g. Swift, require that all properties are initialised > before the constructor returns, but retrofitting this to PHP was > considered impractical, so instead it was left to a run-time error: if > you fail to initialise a property, you will get an error trying to > access it. > > To track that, the engine has to record a special state, but assigning a > meaning to that error state is a bit like using exceptions for flow > control. It would be more in keeping with the original purpose to have > an object_is_valid() function, which returned false if *any* property > had not been initialised to a valid value. > > > PHP actually has a bewildering variety of such special states. In a > different compromise added at the same time, calling unset() on a typed > property puts it into a *separate* state where magic __get and __set are > called, which they are not if the property has simply not yet been > assigned, e.g. https://3v4l.org/C7rIF > > I have always found this a mess. If a property says it is of type > "?int", I want to know that it will always be an integer or null, not > "int or null or uninitialised or unset". If it needs more than one > non-integer state, that should be specified in the type system, e.g. > "int|NotApplicable|NotSpecified|NotLoaded". > > I get your point. Now I see "uninitialised" as a dangerous thing. Unfortunately this state exists and I don't see any general way to avoid objects with these properties. Fighting the "uninitialised" is something big that we may try to discuss in another thread. We have to deal with this state and a syntax to reliably check those invalid properties will be helpful. Something like "$objectVar->property is uninitialized" will be awesome. Luigi Cardamone