Re: Re: Proposal for RFC to remove undefined arrayindexwarning when used ina ternary

From: Date: Sun, 27 Feb 2022 15:31:00 +0000
Subject: Re: Re: Proposal for RFC to remove undefined arrayindexwarning when used ina ternary
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-117154@lists.php.net to get a copy of this message
On Sun, Feb 27, 2022, at 9:11 AM, Robert Landers wrote: > On Sun, Feb 27, 2022 at 12:35 PM Mark Randall <marandall@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

« previous php.internals (#117154) next »