代码之家  ›  专栏  ›  技术社区  ›  Mathieu Mourareau

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

php
  •  0
  • Mathieu Mourareau  · 技术社区  · 4 年前

    当我从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);
    
    0 回复  |  直到 4 年前
        1
  •  3
  •   Yash    4 年前

    这可能是解决方案之一。首先将函数名存储在变量中,然后使用它。

    while (!$template) {
                $temp=$functions[$i];
                $template = $this->$temp($name,$values);
                ++$i;
     }
    
        2
  •  2
  •   Shammi Shailaj    4 年前

    我不确定这是否是最优雅的解决方案,但它会奏效:

    protected function getobjectTemplate($name, $value)
        {
            $template = false;
            $functions = [
                'getObjectFormClientTemplate',
                'getObjectFormTemplate',
                'getObjectAirformTemplate',
                'getTypeAirformTemplate',
                'getAirfileTemplate',
                'getTextAirformTemplate',
            ];
            $i = 0;
            while (!$template) {
                $func = [ $this, $functions[$i] ];
                $template = $func($name, $value);
                ++$i;
            }
    
            return $template;
        }
    

    我可能还会继续删除 while (!template) 条件,因为它有可能使您的代码进入无限循环。可能会使用更好的条件,比如 $i < count($functions) 或者更好的是,比如:

    $i = 0;
    $funcCount = count($functions);
    while(!$template && $i < $funcCount){
       # ...
       ++$i;
    }
    

    此外,您只返回通过调用的所有函数的最后一个值 return $template 。如果你只需要返回最后一个值,为什么不只调用所需的函数而不进行循环呢。不确定,循环是否是最好的方法。如果你提供更多代码的细节,会有所帮助。