On Sun, Feb 27, 2022, at 9:11 AM, Robert Landers wrote: > On Sun, Feb 27, 2022 at 12:35 PM Mark Randall <marand...@php.net> wrote: > >> On 27/02/2022 09:12, Robert Landers wrote: >> > I'd also venture that this warning has caused more harm than good, in >> that >> > writing "$var['something'] ?? null" is second nature when writing new >> > code, even if there is practically no chance for a non-existent key. >> >> >> >> Using null coalesce should only be used when you know you have the >> possibility of a missing key. >> > > That can't always be known, or requires tracing the stack to see where > it comes from and what modifies the array before it is passed to the > function you are modifying, it is an array after all, not a formalized > object with well-defined properties (which I'd prefer, but we can't always > change what someone did 15 years ago just for the sake of changing it).
If you want to assign defaults to missing array keys, this is a generally good approach I've used with much success: $arr = ['a' => 1, 'b' => 2]; $defaults = ['a' => 3, 'c' => 4]; $arr += $defaults; If you're stuck with a pseudo-object hash like that, this is a clean way to guarantee you have values for each key. That said, I'm also open to discussing null-safe array operators. I can see lots of uses for that. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php