8000 Fix type juggling notes for list() and array destructuring by tgr · Pull Request #3680 · php/doc-en · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix type juggling notes for list() and array destructuring #3680

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tgr
Copy link
@tgr tgr commented 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():

array notation:

Fixes #3640.

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.
Array dereferencing a scalar value which is not a <type>string</type>
yields &null;. Prior to PHP 7.4.0, that did not issue an error message.
As of PHP 7.4.0, this issues <constant>E_NOTICE</constant>;
as of PHP 8.0.0, this issues <constant>E_WARNING</constant>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was about array dereferencing ($a = 42; $a[0]) not destructuring ([$a] = 42;), and I think it was correct. Only destructuring weirdly yields null with no warning.

</note>
<note>
<para>
Attempting to access element of a scalar value yields &null;.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say: "Attempting to unpack a scalar value assigns null to all variables." to be consistent with the existing wording.

There's also already a note above: "Strings can not be unpacked" which should probably say "Only arrays and objects that implement ArrayAccess can be unpacked".

And an example below, where the comment could be clarified:

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL

Copy link
Member
@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it has taken us so long to review this; thanks for the patch, one small issue (and please rebase)

yields &null;. Prior to PHP 7.4.0, that did not issue an error message.
As of PHP 7.4.0, this issues <constant>E_NOTICE</constant>;
as of PHP 8.0.0, this issues <constant>E_WARNING</constant>.
Dereferencing a scalar value ields &null;.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Dereferencing a scalar value ields &null;.
Dereferencing a scalar value yields &null;.

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.

Destructuring assignment from null does not raise warning
3 participants
0