Skip to content

Instantly share code, notes, and snippets.

@suin
Created February 25, 2012 15:47
Show Gist options
  • Save suin/1909167 to your computer and use it in GitHub Desktop.
Save suin/1909167 to your computer and use it in GitHub Desktop.
PHPのクロージャで $this を使う方法 ref: http://qiita.com/items/2810
<?php
class Foo
{
public function getClosure()
{
$that = $this;
$closure = function() use ($that) {
$that->bar();
};
return $closure;
}
public function bar()
{
echo 'Called bar method.', PHP_EOL;
}
}
$foo = new Foo();
$closure = $foo->getClosure();
$closure(); // Called bar method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment