如何调用一个类变量的闭包? [英] How to call a closure that is a class variable?
问题描述
class MyClass {
var $ lambda;
function __construct(){
$ this-> lambda = function(){echo'hello world';};
//这里没有错误,所以我认为这是合法的
}
}
$ myInstance = new MyClass();
$ myInstance-> lambda();
//致命错误:调用未定义的方法MyClass :: lambda()
在PHP中,方法和属性在一个单独的命名空间(你可以有一个单独的命名空间方法和具有相同名称的属性),以及您是否正在访问属性或方法取决于您使用的语法。
$ expr-> something()
是一个方法调用,因此PHP将在类中搜索一个方法。
$ expr-> something
是属性提取,因此PHP将在类中搜索属性。
$ myInstance-> lambda();
被解析为一个方法调用,所以PHP在你的方法中搜索一个名为 lambda
的方法类,但没有这样的方法(因此调用未定义的方法错误)。
所以你必须使用 fetch属性语法来获取lambda,然后调用它。
-
c> - > lambda-> __ invoke():
$ myInstance = new MyClass );
$ myInstance-> lambda-> __ invoke();
__ invoke
是 PHP的魔法。当一个对象实现这个方法时,它变得可调用:它可以使用$ var()
语法调用。匿名函数是Closure
的实例,实现__invoke
。 -
或将其指定给本地变量:
$ lambda = $ myInstance-> lambda;
$ lambda();
-
或使用call_user_func调用:
call_user_func($ myInstance-> lambda);
call_user_func
可以调用任何 -
或者,如果这是代码中的常见模式,您可以设置
__ call
方法将呼叫转接到您的lambda:class MyClass
{
private $ lambda;
public function __construct()
{
$ this-> lambda = function(){
echoHello world!\\\
;
};
}
public function __call($ name,$ args)
{
return call_user_func_array($ this-> $ name,$ args);
}
}
b
$ myInstance = new MyClass();
$ myInstance-> lambda();
自从PHP 5.4以来,你甚至可以在一个特征:
trait LambdasAsMethods
{
public function __call($ name,$ args)
{
return call_user_func_array - > $ name,$ args);
}
}
class MyClass
{
使用LambdasAsMethods;
private $ lambda;
public function __construct()
{
$ this-> lambda = function(){
echoHello World!\\\
;
};
}
}
$ myInstance = new MyClass();
$ myInstance-> lambda();
class MyClass {
var $lambda;
function __construct() {
$this->lambda = function() {echo 'hello world';};
// no errors here, so I assume that this is legal
}
}
$myInstance = new MyClass();
$myInstance->lambda();
//Fatal error: Call to undefined method MyClass::lambda()
So what is the correct syntax for reaching class variables ?
In PHP, methods and properties are in a separate namespace (you can have a method and a property with the same name), and whether you are accessing a property or a method depends of the syntax you are using to do so.
$expr->something()
is a method call, so PHP will search for a method in the class.
$expr->something
is a property fetch, so PHP will search for a property in the class.
$myInstance->lambda();
is parsed as a method call, so PHP searches for a method named lambda
in your class, but there is no such method (hence the Call to undefined method error).
So you have to use the fetch property syntax to fetch the lambda, and then call it.
You can do this with
->lambda->__invoke()
:$myInstance = new MyClass(); $myInstance->lambda->__invoke();
__invoke
is one of PHP's magic methods. When an object implements this method, it becomes invokable: it can be called using the$var()
syntax. Anonymous functions are instances ofClosure
, which implements__invoke
.Or assign it to a local variable:
$lambda = $myInstance->lambda; $lambda();
Or call it using call_user_func:
call_user_func($myInstance->lambda);
call_user_func
can call anycallable
, including anonymous functions.Alternatively, if this is a common pattern in your code, you can setup a
__call
method to forward calls to your lambda:class MyClass { private $lambda; public function __construct() { $this->lambda = function() { echo "Hello world!\n"; }; } public function __call($name, $args) { return call_user_func_array($this->$name, $args); } }
Now this works:
$myInstance = new MyClass(); $myInstance->lambda();
Since PHP 5.4 you can even do that in a trait:
trait LambdasAsMethods { public function __call($name, $args) { return call_user_func_array($this->$name, $args); } } class MyClass { use LambdasAsMethods; private $lambda; public function __construct() { $this->lambda = function() { echo "Hello World!\n"; }; } } $myInstance = new MyClass(); $myInstance->lambda();
这篇关于如何调用一个类变量的闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!