Created
April 16, 2011 05:48
-
-
Save hiromi2424/922906 to your computer and use it in GitHub Desktop.
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 TestClosure { | |
public $hoge = 'hoge'; | |
public $piyo = 'piyo'; | |
public $fuga = 'fuga'; | |
public function delegate($lambda) { | |
return $lambda($this); | |
} | |
public function piyo() { | |
return $this->delegate(function($this) { | |
return $this->piyo; | |
}); | |
} | |
public function hoge() { | |
return $this->hoge; | |
} | |
} | |
class TestObject { | |
public $test; | |
public function __construct() { | |
$this->test = new TestClosure; | |
} | |
public function callHoge() { | |
return $this->test->delegate(function ($this) { | |
return call_user_func(array($this, 'hoge')); | |
}); | |
} | |
} | |
$obj = new TestObject; | |
echo $obj->callHoge(); | |
$test = new TestClosure; | |
echo $test->delegate(function($this) { | |
return $this->hoge; | |
}); | |
echo "\n"; | |
echo $test->piyo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment