2 回答

TA貢獻1824條經(jīng)驗 獲得超6個贊
這可以是解決方案之一。首先將函數(shù)名稱存儲在變量中,然后使用它。
while (!$template) {
$temp=$functions[$i];
$template = $this->$temp($name,$values);
++$i;
}

TA貢獻1906條經(jīng)驗 獲得超3個贊
我不確定這是否是最優(yōu)雅的解決方案,但它會起作用:
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;
}
我可能還會繼續(xù)刪除該while (!template)條件,因為它有可能使您的代碼進入無限循環(huán)??赡苁褂酶玫臈l件,$i < count($functions)例如:
$i = 0;
$funcCount = count($functions);
while(!$template && $i < $funcCount){
# ...
++$i;
}
此外,您僅返回通過 調(diào)用的所有函數(shù)的最后一個值return $template。如果您只需返回最后一個值,為什么不只調(diào)用所需的函數(shù)而不使用循環(huán)呢?不確定循環(huán)是否是最好的方法。如果您提供代碼的更多詳細信息,將會有所幫助。
- 2 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報