Created
April 3, 2014 13:20
-
-
Save uzulla/9954191 to your computer and use it in GitHub Desktop.
PHP無名関数(クロージャ)の$thisをあとからかきかえるやつ
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 | |
class world { | |
public $v; | |
public function main(){ | |
$this->v = 'じゃばい'; | |
$f = function(){ | |
echo $this->v; | |
}; | |
$f(); | |
} | |
} | |
$world = new World; | |
$world->main(); |
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 | |
class Box { | |
public $v; | |
public function __construct($str){ | |
$this->v = $str; | |
} | |
public function __invoke(){ | |
return function(){ | |
echo $this->v; | |
}; | |
} | |
} | |
$b = new Box('Yeah!'.PHP_EOL); | |
$f = $b(); // make Object related closure | |
$f(); // => yeah! | |
$f = $f->bindTo(new Box('Damn!'.PHP_EOL)); | |
$f();// => Damn! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment