【发布时间】:2013-02-28 15:57:55
【问题描述】:
function convert($currencyType)
{
$that = $this;
return $result = function () use ($that)
{
if (!in_array($currencyType, $this->ratio))
return false;
return ($this->ratio[$currencyType] * $this->money); //a float number
};
}
$currency = new Currency();
echo $currency->convert('EURO');
怎么了?
我收到错误消息:
Catchable fatal error: Object of class Closure could not be converted to string
【问题讨论】:
-
您的
convert()函数返回一个函数。然后你试图通过echo()ing 将它强制转换成一个字符串。 -
但是闭包返回一个float/false给$result?
-
而你在闭包中的
$this引用应该是$that。他们可能在 5.4 的思想中改变了这一点;不确定。 -
您实际上必须调用生成的闭包来获取浮动。你为什么在这里使用闭包?
-
啊。让我给你一个正式的答案……
标签: php