Skip to content

Instantly share code, notes, and snippets.

@igorw
Created May 1, 2011 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorw/950838 to your computer and use it in GitHub Desktop.
Save igorw/950838 to your computer and use it in GitHub Desktop.
Closures in PHP 5.2.
<?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