[英]php Closure object how to read it?
I have this code but I am stuck... 我有这个代码,但我被卡住了......
$my_var = function (){
return array('hello you');
};
var_dump($my_var); // returns object(Closure)#2 (0) { }
how do I echo $my_var
? 我怎么
echo $my_var
?
I would assume it would be echo $my_var[0]
; 我认为它将是
echo $my_var[0]
; but this does not work. 但这不起作用。
Fatal error: Cannot use object of type Closure as array in ...
A closure is a function. 闭包是一种功能。 Therefore you have to call it, like this :
因此你必须这样称呼它,如下所示:
$myvar();
Since php5.4 with Array Access: 从php5.4开始使用Array Access:
echo $myvar()[0];
$my_var represents a function. $ my_var代表一个函数。 You need to call it first to get the return value.
您需要先调用它才能获得返回值。
尝试print_r它会打印数组或对象
print_r($my_var);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.