Created
February 25, 2012 15:47
-
-
Save suin/1909167 to your computer and use it in GitHub Desktop.
PHPのクロージャで $this を使う方法 ref: http://qiita.com/items/2810
This file contains hidden or 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 | |
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