php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62918 anonymous function/closure use variable problem
Submitted: 2012-08-24 09:08 UTC Modified: 2013-06-29 20:15 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: nilsandre at gmx dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.6 OS: Debian 6, Kernel 2.6.32-5-686
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nilsandre at gmx dot de
New email:
PHP Version: OS:

 

 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-29 20:15 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug
 [2013-06-29 20:15 UTC] 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.
 [2017-10-27 11:22 UTC] acvostok at outlook dot com
Example code to reproduce:

https://stackoverflow.com/questions/46973528/php-dynamic-var-in-use?noredirect=1#comment80894921_46973528
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 14:01:30 2025 UTC