Skip to content

Throw notice on array offset access of null/bool/int/etc #4386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Jul 10, 2019

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.

@nikic nikic added this to the PHP 7.4 milestone Jul 10, 2019
@nikic nikic changed the base branch from master to PHP-7.4 July 10, 2019 09:36
@nikic
Copy link
Member Author

nikic commented Jul 10, 2019

Merged as c42b7dd and 26431d1.

@ondrejmirtes
Copy link
Contributor

Unfortunately this is still allowed after this PR :/

$null = null;
$null['foo'] = 1; // $null is an array now

@carusogabriel
Copy link
Contributor

@ondrejmirtes Indeed: https://3v4l.org/eLmFq. Thanks for the heads up, we'll take a look.

@claudepache
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants