Created
May 1, 2011 20:29
-
-
Save igorw/950838 to your computer and use it in GitHub Desktop.
Closures in PHP 5.2.
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 | |
function create_closure($args, $closureMap, $code) { | |
if ($closureMap) { | |
$globalRand = mt_rand(); | |
$GLOBALS[$globalRand] = $closureMap; | |
$code = 'extract($GLOBALS['.$globalRand.']);'.$code; | |
} | |
return create_function($args, $code); | |
} | |
$outer = 1; | |
$anon = create_closure('$a, $b', compact('outer'), 'return $a + $b + $outer;'); | |
echo $anon(2, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment