Created
September 3, 2013 06:56
-
-
Save milo/6420469 to your computer and use it in GitHub Desktop.
Unexpected PHP closure behaviour, at least for me.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$var = 'INIT'; | |
$fGet = function() use ($var) { | |
echo "$var\n"; | |
}; | |
$fSet = function($i) use (& $var) { | |
$var = "I: $i"; | |
}; | |
for ($i = 0; $i < 5; $i++) { | |
$fGet(); | |
$fSet($i); | |
} | |
/* | |
RESULT: | |
======= | |
INIT | |
INIT | |
INIT | |
INIT | |
INIT | |
*/ |
@hrach Pravda, takhle je to jasné. Jen jsem si use
bez ampersandu překládal jako měj přístup k
. Přišlo mi přirozené, že se proměnná přenese do scope funkce. Měl jsem se držet doslovného použij
. :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zacne to davat smysl, kdyz si misto clousury predstavis: