从php5.6到7.1数组到字符串的转换
当我从5.6升级到7.1时,这一行有一个“数组到字符串转换”通知:从php5.6到7.1数组到字符串的转换,php,Php,当我从5.6升级到7.1时,这一行有一个“数组到字符串转换”通知: $template = $this->$functions[$i]($name, $value); 为了使用php7.1,我如何解决它 更新: protected function getobjectTemplate($name, $value) { $template = false; $functions = [ 'getObjectFormClie
$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);
我不确定这是否是最优雅的解决方案,但它会起作用:
受保护的函数getobjectTemplate($name,$value)
{
$template=false;
$functions=[
“getObjectFormClientTemplate”,
“getObjectFormTemplate”,
“GetObjectArmTemplate”,
“GetTypeArmTemplate”,
“getAirfileTemplate”,
“GetTextTemplate”,
];
$i=0;
while(!$template){
$func=[$this,$functions[$i]];
$template=$func($name,$value);
++$i;
}
返回$template;
}
我可能还会继续删除while(!template)
条件,因为它有可能使代码进入无限循环。可能会使用更好的条件,如$i
,或者更好的条件,例如:
$i=0;
$funcCount=count($functions);
while(!$template&&$i<$funcount){
# ...
++$i;
}
另外,您只返回通过
return$template
调用的所有函数的最后一个值。如果您只需要返回最后一个值,为什么不只调用所需的函数而不使用循环呢。不确定是否有一个循环是最好的方法。如果您提供代码的更多详细信息,将会有所帮助。这可能是解决方案之一。首先将函数名存储在变量中,然后使用它
while (!$template) {
$temp=$functions[$i];
$template = $this->$temp($name,$values);
++$i;
}
我们不知道什么是
$functions[$i]
的代码,以及$name
和$value
变量是什么类型的,您能添加更多详细信息吗?您正在尝试将数组转换为字符串,您应该尝试阻止YOR代码这样做。但我们无法从提供的最少代码(以及数据的完全缺乏)中准确地判断问题在哪里。$name和$value是字符串datajust updated@kaddath这只是调用的函数的名称,而不是它们的代码,你能添加引起此通知的函数的代码吗?关于无限循环,你是对的,但我猜是(!$template){
条件是循环在返回结果的第一个函数处停止。若要保持此行为,更正的版本应为,而(!$template&&$i<$funcCount){