Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created December 11, 2013 09:59
Show Gist options
  • Save ritalin/7907842 to your computer and use it in GitHub Desktop.
Save ritalin/7907842 to your computer and use it in GitHub Desktop.
// こんなふうに、外側の変数を束縛すると、関数定義を変更する必要が無くなる
function outer() {
$n = 10;
$m = 200;
my_func(function($i, $x) use($n) { return $n + $n }, [10, 20, 30]);
my_func(function($i, $x) use($n, $m) { return $n + $n * $m }, [10, 20, 30]);
}
function mt_func(closure $callback, array $a) {
foreach ($a as $x) {
echo $callback($x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment