Bug #78970 [NEW]: Compact can't resolve outer scoped variables using short closures
| From: | stanislav dot goldmann at gmail dot com | Date: | Mon, 16 Dec 2019 13:37:14 +0000 |
| Subject: | Bug #78970 [NEW]: Compact can't resolve outer scoped variables using short closures | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-224340@lists.php.net to get a copy of this message | ||
From: stanislav dot goldmann at gmail dot com
Operating system: Linux/Any
PHP version: 7.4.0
Package: Arrays related
Bug Type: Bug
Bug description:Compact can't resolve outer scoped variables using short closures
Description:
------------
In the proposal of short closures
(https://wiki.php.net/rfc/arrow_functions_v2) the following is stated:
"When a variable used in the expression is defined in the parent scope
it will be implicitly captured by-value."
With this example:
$y = 1;
$fn1 = fn($x) => $x + $y;
$fn2 = function ($x) use ($y) {
return $x + $y;
};
These two behave exactly the same, as expected.
However compact doesn't behave that way, as it doesn't recognize a
variable from outer scope until it is explicitly called.
Test script:
---------------
$name = 'foo';
$array = ['bar'];
$failing = fn($value) => compact('name', 'value');
$working = fn($value) => compact('name', 'value') + [$name];
var_dump(array_map($failing, $array));
var_dump(array_map($working, $array));
Expected result:
----------------
Short closures using compact should resolve outer scoped variables and
behave like "long" closures using an use statement.
fn($value) => compact('name', 'value')
should work just like
function($value) use ($name) {
return compact('name', 'value');
}
And return the following
array(1) {
[0]=>
array(3) {
["name"]=>
string(3) "foo"
["value"]=>
string(3) "bar"
}
}
Actual result:
--------------
// Notice: compact(): Undefined variable: name
array(1) {
[0]=>
array(1) {
["value"]=>
string(3) "bar"
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=78970&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=78970&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78970&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78970&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78970&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78970&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78970&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78970&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78970&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78970&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78970&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78970&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=78970&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78970&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78970&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78970&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78970&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78970&r=mysqlcfg