note 119733 added to reflectionmethod.getclosure
| From: | okto | Date: | Sun, 14 Aug 2016 10:45:39 +0000 |
| Subject: | note 119733 added to reflectionmethod.getclosure | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206283@lists.php.net to get a copy of this message | ||
Use method from another class context.
<?php
class A {
private $var = 'class A';
public function getVar() {
return $this->var;
}
public function getCl() {
return function () {
$this->getVar();
};
}
}
class B {
private $var = 'class B';
}
$a = new A();
$b = new B();
print $a->getVar() . PHP_EOL;
$reflection = new ReflectionClass(get_class($a));
$closure = $reflection->getMethod('getVar')->getClosure($a);
$get_var_b = $closure->bindTo($b, $b);
print $get_var_b() . PHP_EOL;
// Output:
// class A
// class B
----
Server IP: 72.52.91.14
Probable Submitter: 185.113.38.204
----
Manual Page -- http://php.net/manual/en/reflectionmethod.getclosure.php
Edit -- https://master.php.net/note/edit/119733
Del: integrated -- https://master.php.net/note/delete/119733/integrated
Del: useless -- https://master.php.net/note/delete/119733/useless
Del: bad code -- https://master.php.net/note/delete/119733/bad+code
Del: spam -- https://master.php.net/note/delete/119733/spam
Del: non-english -- https://master.php.net/note/delete/119733/non-english
Del: in docs -- https://master.php.net/note/delete/119733/in+docs
Del: other reasons-- https://master.php.net/note/delete/119733
Reject -- https://master.php.net/note/reject/119733
Search -- https://master.php.net/manage/user-notes.php