Bug #62918 [Com]: anonymous function/closure use variable problem
| From: | acvostok at outlook dot com | Date: | Fri, 27 Oct 2017 11:22:49 +0000 |
| Subject: | Bug #62918 [Com]: anonymous function/closure use variable problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212338@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62918&edit=1
ID: 62918
Comment by: acvostok at outlook dot com
Reported by: nilsandre at gmx dot de
Summary: anonymous function/closure use variable problem
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Debian 6, Kernel 2.6.32-5-686
PHP Version: 5.4.6
Block user comment: N
Private report: N
New Comment:
Example code to reproduce:
https://stackoverflow.com/questions/46973528/php-dynamic-var-in-use?noredirect=1#comment80894921_46973528
Previous Comments:
------------------------------------------------------------------------
[2013-06-29 20:15:26] yohgaki@php.net
$printGlob2 = function() use (${'x'}) {
This is obvious syntax error. PHP does not support ${'x'} in script context.
${var} notation is only used for text context to distinguish text and variable.
------------------------------------------------------------------------
[2012-08-24 09:08:04] nilsandre at gmx dot de
Description:
------------
In an anonymous function, the use clause does not accept a variable that is denoted as
${'variable'}, whereas the equivalent $variable of course works.
See test script below.
Test script:
---------------
$x = 1; // global scope
$printGlob = function() use ($x) {
echo "x = $x".PHP_EOL;
};
$printGlob(); // success, prints x = 1
$x = 2;
$printGlob2 = function() use (${'x'}) {
// Preceeding line yields:
// Parse error: syntax error, unexpected '$', expecting '&' or variable
(T_VARIABLE)
echo "x2 = $x".PHP_EOL;
};
$printGlob2(); // Should parse, and yield x2 = 2
Expected result:
----------------
$f = function() use ($var) { ... };
and
$f = function() use (${'var'}) { ... };
should be considered equivalent and therefore parse properly.
Actual result:
--------------
See test script.
$f = function() use (${'var'}) { ... };
yields a parse error, which in addition is quite confusing.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62918&edit=1