猿问

从php5.6到7.1数组到字符串的转换

当我从 5.6 升级到 7.1 时,我收到此行的“数组到字符串转换”通知:


 $template = $this->$functions[$i]($name, $value); 

我如何解决它才能与 php7.1 一起使用?


更新 :


protected function getobjectTemplate($name, $value)

    {

        $template = false;

        $functions = [

            'getObjectFormClientTemplate',

            'getObjectFormTemplate',

            'getObjectAirformTemplate',

            'getTypeAirformTemplate',

            'getAirfileTemplate',

            'getTextAirformTemplate',

        ];

        $i = 0;

        while (!$template) {

            $template = $this->$functions[$i]($name, $value);

            ++$i;

        }


        return $template;

    }

这里调用了 getobjectTemplate 方法


$template = $this->getobjectTemplate($name, $value);


噜噜哒
浏览 106回答 2
2回答

慕妹3242003

这可以是解决方案之一。首先将函数名称存储在变量中,然后使用它。while (!$template) {            $temp=$functions[$i];            $template = $this->$temp($name,$values);            ++$i; }

一只名叫tom的猫

我不确定这是否是最优雅的解决方案,但它会起作用:protected function getobjectTemplate($name, $value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $template = false;&nbsp; &nbsp; &nbsp; &nbsp; $functions = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getObjectFormClientTemplate',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getObjectFormTemplate',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getObjectAirformTemplate',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getTypeAirformTemplate',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getAirfileTemplate',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'getTextAirformTemplate',&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (!$template) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $func = [ $this, $functions[$i] ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template = $func($name, $value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++$i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $template;&nbsp; &nbsp; }我可能还会继续删除该while (!template)条件,因为它有可能使您的代码进入无限循环。可能使用更好的条件,$i < count($functions)例如:$i = 0;$funcCount = count($functions);while(!$template && $i < $funcCount){&nbsp; &nbsp;# ...&nbsp; &nbsp;++$i;}此外,您仅返回通过 调用的所有函数的最后一个值return $template。如果您只需返回最后一个值,为什么不只调用所需的函数而不使用循环呢?不确定循环是否是最好的方法。如果您提供代码的更多详细信息,将会有所帮助。
随时随地看视频慕课网APP
我要回答