Skip to content
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

Destructuring assignment from null does not raise warning #3640

Open
tgr opened this issue Aug 5, 2024 · 1 comment · May be fixed by #3680
Open

Destructuring assignment from null does not raise warning #3640

tgr opened this issue Aug 5, 2024 · 1 comment · May be fixed by #3680
Labels
bug Documentation contains incorrect information

Comments

@tgr
Copy link

tgr commented Aug 5, 2024

https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.destructuring says

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_WARNING-level error message (E_NOTICE-level prior to PHP 8.0.0) will be issued, and the result will be null.

but this isn't actually true when assigning null; [$x, $y] = null; does not raise a notice or warning (eval). Same goes for destructuring with an associative array (eval). This seems to be specific to null (e.g. [$x, $y] = []; raises a warning: eval) and to destructuring assignment (e.g. null[0] raises a warning: eval).

Not sure if this is a bug or a documentation issue, but either there should be a warning or this special case should be documented.

@cmb69
Copy link
Member

cmb69 commented Aug 7, 2024

Well, let's say it's a feature, but document it (also for list()).

@cmb69 cmb69 added the bug Documentation contains incorrect information label Aug 7, 2024
tgr added a commit to tgr/doc-en that referenced this issue Aug 24, 2024
tgr added a commit to tgr/doc-en that referenced this issue Aug 24, 2024
Update notes about the handling of missing keys and non-arrays
during array destructuring to match the behavior seen on
3v4l.org, which doesn't issue a warning or notice in some cases
where the documentation claims it does. Also update list() docs
to provide the same information.

list():
* missing key: https://3v4l.org/bYBt6
* destructuring an integer: https://3v4l.org/7lNhK
* a boolean: https://3v4l.org/p5LDd
* a string: https://3v4l.org/KHSAH
* null: https://3v4l.org/lk1iu

array notation:
* missing key: https://3v4l.org/KCiTC
* destructuring an integer: https://3v4l.org/louVG
* a boolean: https://3v4l.org/SWL5L
* a string: https://3v4l.org/aK90L
* null: https://3v4l.org/Y8cOY

Fixes php#3640.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation contains incorrect information
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants