note 96341 added to functions.anonymous
| From: | ljackson at jjcons dot com | Date: | Mon, 22 Feb 2010 06:46:26 +0000 |
| Subject: | note 96341 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166244@lists.php.net to get a copy of this message | ||
appears kwilson at shuttlebox dot net that you may have just made unintended side effect. Note that
adding the global $variable to your test function make the closure function echo second rather than
first So the anonymous function works as expected with respect to globals.
<?php
$variable = "first";
$closure = function() {
global $variable;
echo $variable . "\n";
};
$closure();
function test($closure)
{
global $variable; //Note the scope added here
$variable = "second";
$closure();
}
test($closure);
?>
prints:
first
second
tested with php 5.3.1
----
Server IP: 192.41.40.227
Probable Submitter: 67.191.151.222
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/96341
Del: integrated -- https://master.php.net/note/delete/96341/integrated
Del: useless -- https://master.php.net/note/delete/96341/useless
Del: bad code -- https://master.php.net/note/delete/96341/bad+code
Del: spam -- https://master.php.net/note/delete/96341/spam
Del: non-english -- https://master.php.net/note/delete/96341/non-english
Del: in docs -- https://master.php.net/note/delete/96341/in+docs
Del: other reasons-- https://master.php.net/note/delete/96341
Reject -- https://master.php.net/note/reject/96341
Search -- https://master.php.net/manage/user-notes.php