#50814 [Opn->Bgs]: Anonymous functions pull global variables from wrong scope
| From: | johannes@php.net | Date: | Thu, 21 Jan 2010 15:28:29 +0000 |
| Subject: | #50814 [Opn->Bgs]: Anonymous functions pull global variables from wrong scope | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-146850@lists.php.net to get a copy of this message | ||
ID: 50814
Updated by: johannes@php.net
Reported By: kwilson at shuttlebox dot net
-Status: Open
+Status: Bogus
Bug Type: Unknown/Other Function
Operating System: Linux
PHP Version: 5.3.1
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
global imports from the global scope, not the caller's or such.
Previous Comments:
------------------------------------------------------------------------
[2010-01-21 15:14:35] kwilson at shuttlebox dot net
Description:
------------
If an anonymous function pulls a variable from the global scope using
"global", and that function is passed to another scope, it pulls the
variable from the scope where the function was created, not where it was
executed.
Reproduce code:
---------------
<?php
$variable = "first";
$closure = function() {
global $variable;
echo $variable . "\n";
};
$closure();
function test($closure)
{
$variable = "second";
$closure();
}
test($closure);
?>
Expected result:
----------------
first
second
Actual result:
--------------
first
first
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50814&edit=1