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

From: Date: Sun, 27 Feb 2022 11:35:41 +0000
Subject: Re: Re: Proposal for RFC to remove undefined arrayindexwarning when used ina ternary
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-117151@lists.php.net to get a copy of this message
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. For everything else you've probably entered an unexpected state and should display a warning (that should ideally be picked up by an exception handler and convert it to an exception). When handling external data it is usually a good idea to use a validation step before using it, such as JSON schema. An even better idea is to parse it into a specific type, rather than just validating it, at which point you're going to be using a lot of isset, null coalesces, array_key_exists, property_exists etc anyway and sucking in some default other than null is almost always the wrong thing to do. As to your final point: PHP internals voted by a supermajority to raise this from a notice to a warning in PHP 8, it seems unlikely that 33% of people are now going to change their mind and vote for the opposite.

« previous php.internals (#117151) next »