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.


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to