On Mon, Jun 24, 2024 at 7:28 PM Chuck Adams <chaz@chaz.works> wrote: > > > > On Jun 24, 2024, at 3:31 AM, Robert Landers <landers.rob...@gmail.com> > > wrote: > > > > > > There's no need to use `?` to check for existence on a key, so this: > > > > $arr is ['a' => string, ?'b' => string, ...]; > > > > should be this: > > > > $arr is ['a' => string, 'b' => ?string, ...]; > > > > because $arr['non-existent-key'] is NULL. > > > > > The first means b is an optional key, but if it’s there, can only be a > string. The second says b is a required key, but it may be a string or null. > If there were a binding involved, that determines the type of the binding in > incompatible ways. I’m fine with requiring bindings to be nullable for > optional keys, but it strikes me as strictly less flexible and not consistent > with the rest of PHP’s behavior, at least not under E_ALL. >
Hi Chuck, To be honest, this is one of the smaller concerns I have with the new syntax. There might be some misunderstanding here, though. A non-existent key is NULL, always has been, and always will be. No current RFCs are stating otherwise, to my knowledge. While a warning is emitted when accessing a non-existent key, and some people trigger exceptions or fatal errors on warnings, this isn’t universal, and it isn’t always an error (after all, the value is NULL, which might be a properly handled case). If you see the warning, it’s usually a hint that something might be wrong, but often, a quick ?? is all you need to make the warning go away, further reinforcing that the value of a non-existent key is NULL. $arr = ['a' => 'a string']; $arr is ['a' => string, ?'b' => $value, ...]; This syntax implies that a non-existent key is a special case, and if it passes as-is, it will be. If there is a binding and the key is missing, what happens to that binding? Is it left in an undefined state or is it assigned NULL? If it is assigned NULL, what is the value of checking for non-existence; why not just check for NULL in the value? If it is left "undefined", then you still have to handle it as though it is a NULL value (perhaps more so because simply trying to detect its value may cause the very warning you are trying to avoid -- and will fatally error in PHP 9: https://3v4l.org/Rbadk. see https://wiki.php.net/rfc/undefined_variable_error_promotion). Thus, this "key existence check" is inconsistent with the language and possibly redundant, as you'd have to check that resulting value was null anyway: $arr is ['a' => string, 'b' => ?string, ...]; Hope that clarifies things.