-
-
Save funkatron/860335 to your computer and use it in GitHub Desktop.
<?php | |
// hey dude | |
$boom = 'flazm'; | |
$heydude = function ($name) use ($boom) { | |
echo "Hey {$name}: {$boom}"; | |
}; | |
$boom = 'flar filth filth flar filth'; | |
$heydude("larry"); |
@ranza I suppose that's in the eye of the beholder. JS does not have the declaration-time limitation: http://jsfiddle.net/d7TTE/
If you know you want the value to change based on what it is when you call the lambda, you need to use references or objects:
$boom = 'flazm';
$heydude = function ($name) use (&$boom) {
echo "Hey {$name}: {$boom}";
};
// ...
Or:
$boom = new stdClass();
$boom->message = 'flazm';
$heydude = function ($name) use ($boom) {
echo "Hey {$name}: {$boom->message}";
}
Make sense?
make sense, thanks @weierophinney!
Nevermind... What matthew said is better :)
Matthew's example with the reference is best if you don't want to use objects. Put a different way, with a different solution pulling from the GLOBALS symbol table:
$heydude = function ($name) /* use($boom) imports from declaration time symbol table */ { global $boom; // import from at-runtime symbol table ... };
I threw up a little in my mouth when I saw "global"
PHP doesnt know you are defining $boom later on. When you set $heydude it reads the current available variable and saves that in one scope. What ever comes after that is unknown
This makes perfect sense :)