-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Break reference after foreach by reference #7364
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems useful to me to have as the RFC currently is to avoid a common source of bugs.
The intended behavior of break 2;
and continue 2;
or goto
should probably be noted (i.e. references are only converted to variables if the end of the loop is reached normally
And what if someone is intentionally using this behavior? I'm not, but to act like no one could is ridiculous. Is it a potential footgun? Yes. Should all footguns be prevented? No. |
I think you can put a safety lock to it by making the user do something like foreach ($a as &$x) {
$xRef = &$x;
}
$xRef = ... if they really want to use it |
This is acknowledged in https://wiki.php.net/rfc/foreach_unwrap_ref#backward_incompatible_changes. Also, please leave RFC feedback on the PHP internals mailing list rather than GitHub. |
It's 2021, a mailing list is kinda archaic. |
I hadn't originally considered this, but to me it would make the most sense to have the same behavior even if multi-level break / goto is used. Will have to push the reference unwrapping to the unwind stack. |
0ffe0bc
to
8bd6889
Compare
After `foreach ($array as &$val) {}` perform a reference-unwrap on `$val`. This means that it will become a normal value rather than a reference to the last element of `$array`. This fixes the common WTF where foreach ($array as &$val) {} foreach ($array as $val) {} will change the second-to-last element of the array to be the same as the last element.
This RFC has been withdrawn. |
After
foreach ($array as &$val) {}
perform a reference-unwrapon
$val
. This means that it will become a normal value ratherthan a reference to the last element of
$array
.This fixes the common WTF where
will change the second-to-last element of the array to be the
same as the last element.
RFC: https://wiki.php.net/rfc/foreach_unwrap_ref