Bug #62918 [NEW]: anonymous function/closure use variable problem

From: Date: Fri, 24 Aug 2012 09:08:04 +0000
Subject: Bug #62918 [NEW]: anonymous function/closure use variable problem
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-172864@lists.php.net to get a copy of this message
From: nilsandre at gmx dot de Operating system: Debian 6, Kernel 2.6.32-5-686 PHP version: 5.4.6 Package: Scripting Engine problem Bug Type: Bug Bug description:anonymous function/closure use variable problem 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 bug report at https://bugs.php.net/bug.php?id=62918&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=62918&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=62918&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=62918&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=62918&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62918&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=62918&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=62918&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=62918&r=needscript Try newer version: https://bugs.php.net/fix.php?id=62918&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=62918&r=support Expected behavior: https://bugs.php.net/fix.php?id=62918&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=62918&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=62918&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=62918&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=62918&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=62918&r=dst IIS Stability: https://bugs.php.net/fix.php?id=62918&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=62918&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=62918&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=62918&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=62918&r=mysqlcfg

« previous php.bugs (#172864) next »