Skip to content

Instantly share code, notes, and snippets.

@uzulla
Created April 3, 2014 13:20
Show Gist options
  • Save uzulla/9954191 to your computer and use it in GitHub Desktop.
Save uzulla/9954191 to your computer and use it in GitHub Desktop.
PHP無名関数(クロージャ)の$thisをあとからかきかえるやつ
<?php
class world {
public $v;
public function main(){
$this->v = 'じゃばい';
$f = function(){
echo $this->v;
};
$f();
}
}
$world = new World;
$world->main();
<?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