Req #81417 [Wfx]: Option to determine if reading an undefined array key should throw a warning

From: Date: Wed, 09 Feb 2022 07:00:45 +0000
Subject: Req #81417 [Wfx]: Option to determine if reading an undefined array key should throw a warning
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-239564@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81417&edit=1 ID: 81417 User updated by: nicolas at gestixi dot com Reported by: nicolas at gestixi dot com Summary: Option to determine if reading an undefined array key should throw a warning Status: Wont fix Type: Feature/Change Request Package: *Configuration Issues PHP Version: 8.0.10 Block user comment: N Private report: N New Comment: Indeed it would be great to know why it is such a very dangerous door... We still have not started to upgrade our codebase. We have 138627 lines of code to review just because of this "useless" warning. I will have to find a way to automate most of it, or spend weeks doing it… Previous Comments: ------------------------------------------------------------------------ [2022-02-08 17:20:29] k dot andris at gmail dot com "is opening a very big and very dangerous door that really ought to remain closed"... funny. It's a door that's been wide open for many years. It would be helpful requinix, if you'd point us to an RFC or somethine where making this backward incompatible change has been decide instead of lecturing loyal developers. Also keeping them from running away to other languages as I did. Thanks. ------------------------------------------------------------------------ [2021-09-06 01:50:52] requinix@php.net As a rule of thumb, adding another php.ini option is not the right answer. What's more, adding one that tells PHP to ignore certain types of warnings/errors is opening a very big and very dangerous door that really ought to remain closed. If your codebase is 10 years old then you'll have to accept that you cannot simply upgrade PHP versions (especially majors) and expect everything to work as it did back in 2011. PHP 7.4 is the last of the 7.x series and will remain in active support for an extended period - use that time to upgrade your codebase to be compatible with 8.x. ------------------------------------------------------------------------ [2021-09-05 18:41:06] nicolas at gestixi dot com I tried with a custom error handler. Something like: if (strpos($errstr, 'Undefined index:') !== false OR strpos($errstr, 'Undefined offset:') !== false) return true; else return false; But it exhausts the memory before the end of the request. ------------------------------------------------------------------------ [2021-09-05 14:39:51] php-bugs at allenjb dot me dot uk You can already silence this warning in your own code using a custom error handler. See https://www.php.net/set_error_handler You should however keep in mind that warnings tend to get escalated to errors over time (this was a notice since at least 5.4) The null coalescing operator also provides an alternative "fix" that you may find more readable - like isset() it silences warnings about undefined indexes (and properties or variables): https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op In the case you gave, you would write: if ($array['key'] ?? false) ------------------------------------------------------------------------ [2021-09-05 12:22:40] nicolas at gestixi dot com Adjust the summary which was too long. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=81417 -- Edit this bug report at https://bugs.php.net/bug.php?id=81417&edit=1

« previous php.bugs (#239564) next »