Throw notice on array offset access of null/bool/int/etc#4386
Closed
nikic wants to merge 2 commits intophp:PHP-7.4from
Closed
Throw notice on array offset access of null/bool/int/etc#4386nikic wants to merge 2 commits intophp:PHP-7.4from
nikic wants to merge 2 commits intophp:PHP-7.4from
Conversation
SCIF
approved these changes
Jul 10, 2019
Member
Author
Contributor
|
Unfortunately this is still allowed after this PR :/ $null = null;
$null['foo'] = 1; // $null is an array now |
Contributor
|
@ondrejmirtes Indeed: https://3v4l.org/eLmFq. Thanks for the heads up, we'll take a look. |
Contributor
|
@ondrejmirtes @carusogabriel The behaviour is expected, and falls outside the scope of the relevant RFC. This is a feature I take sometimes advantage of; that is, instead of writing: if (!isset($foo[$bar]))
$foo[$bar] = [ ];
$foo[$bar][] = $baz;I just write: $foo[$bar][] = $baz;for the same effect. I expect that much existing code relies on that implicit creation of array. As user, I have no strong feeling on whether it is a good feature for a new language, but I would be quite displeased if perfectly valid code started to throw warnings for unclear benefit. |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a followup to #2031 for RFC https://wiki.php.net/rfc/notice-for-non-valid-array-container.
This implements the dead-simple variant of throwing a notice for array accesses on null/bool/int/float/resource (everything but array, string and object), while excluding list(), for which we did not reach an agreement on #2031. It does not try to suppress duplicate notices on nested accesses, because the additional complexity is not worthwhile.