如果可能的话,我正试图找到让示例2工作的方法。请问有人帮我吗?
拨打
$this->getValue('getName');
$this->getValue('getEmail');
示例1(工作)
private function getValue($method)
{
$o = new Order();
$p = $o->Payment();
return $p->$method(); // Works
return $p->call_user_func($method); // Works
}
示例2(不会工作)
private function getValue($method)
{
return
new Closure(function (Order $o) {
if ($o->getPayment() instanceof Payment) {
return $o->Payment()->$method(); // Don't Work
return $o->Payment()->call_user_func($method); // Don't Work
}
});
}
答案 0 :(得分:1)
class Test {
public function abc(){
echo "ok";
}
}
function getValue($method){
return (function($o) use ($method) {
if ($o instanceof Test) {
return $o->$method();
}
});
}
$m = getValue('abc');
$m(new Test());
答案 1 :(得分:0)
$o
未定义,可能您收到此错误消息:
Parse error: parse error, expecting `'&'' or `T_VARIABLE'
这应该有效:
private function getValue($method)
{
return
new Closure(function () {
$o = new Order();
return $o->Payment()->$method();
});
}