|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-24 09:08 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 19:00:01 2025 UTC |
$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.