php 类 Closure 的对象无法转换为字符串:文件名。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15373072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:07:10  来源:igfitidea点击:

Object of class Closure could not be converted to string in: filename.

php

提问by Lisa Miskovsky

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');

What's wrong?

怎么了?

I'm getting the error message:

我收到错误消息:

Catchable fatal error: Object of class Closure could not be converted to string

回答by landons

Couple of issues:

几个问题:

  1. Because you're returning a closure, you have to first assign the closure to a variable, and then call the function
  2. Your $thisreferences won't work inside a closure (which is why you're useing $thatinstead)
  3. You need to also use $currencyTypeto access it in the closure's scope
  1. 因为要返回一个闭包,所以必须先将闭包赋值给一个变量,然后再调用该函数
  2. 您的$this引用在闭包内不起作用(这就是为什么您要使用useing $that
  3. 您还需要使用$currencyType在闭包的范围内访问它


function convert($currencyType)
{
    $that =& $this; // Assign by reference here!
    return $result = function () use ($that, $currencyType) // Don't forget to actually use $that
    {
        if (!in_array($currencyType, $that->ratio))
                return false;

        return ($that->ratio[$currencyType] * $that->money); //a float number
    };
}

$currency = new Currency();
$convert = $currency->convert('EURO');
echo $convert(); // You're not actually calling the closure until here!

回答by chakroun yesser

You have to make function between parentheses and add parentheses when closing the function.

您必须在括号之间创建函数并在关闭函数时添加括号。

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');

回答by Shoe

Just delete the returnthere and do:

只需删除return那里并执行:

$result = function () use ($that) 
{
    if (!in_array($currencyType, $this->ratio))
            return false;

    return ($this->ratio[$currencyType] * $this->money); //a float number
};
return $result();

Also, are you realizing you are not using $thatinside the function?

另外,您是否意识到您没有$that在函数内部使用?

By the way, why do you need an anonymous function there? Just do:

顺便说一句,为什么你需要一个匿名函数呢?做就是了:

function convert($currencyType)
{
    if (!in_array($currencyType, $this->ratio))
        return false;

    return ($this->ratio[$currencyType] * $this->money); //a float number
}

回答by Matt

class Currency {
    function convert($currencyType)
    {
        if (!in_array($currencyType, $this->ratio))
             return false;
        return ($this->ratio[$currencyType] * $this->money); //a float number
    }
}

$currency = new Currency();
echo $currency->convert('EURO');

You are defining a lambda function. You don't need it. Also, you should be using bcmul()if this is to have any kind of accuracy; floats in PHP will give you funky results.

您正在定义一个 lambda 函数。你不需要它。此外,如果要获得任何准确性,您应该使用bcmul();PHP 中的 floats 会给你带来时髦的结果。