Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created April 16, 2011 05:48
Show Gist options
  • Save hiromi2424/922906 to your computer and use it in GitHub Desktop.
Save hiromi2424/922906 to your computer and use it in GitHub Desktop.
<?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