这个功能
public function confirmation() {
if (is_array($this->modules)) {
if (isset($GLOBALS[$this->selected_module]) &&
is_object($GLOBALS[$this->selected_module]) &&
($GLOBALS[$this->selected_module]->enabled)) {
return $GLOBALS[$this->selected_module]->confirmation();
}
}
}
Run Code Online (Sandbox Code Playgroud)
发出通知
缺少退货声明
是否有任何解决方案可以在括号外获得回报?
Jon*_*Jon 12
你在这里有return一个有条件的内部.如果不满足条件,那么执行将到达函数的末尾而不会发出return语句,这相当于return null.
因此,你可以做的一件事就是return null明确:
if (...) {
return $GLOBALS[$this->selected_module]->confirmation();
}
else {
// Can also do this without an "else", it's a matter of style
return null;
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用三元运算符将条件移动到返回值表达式:
return is_array($this->modules) &&
isset($GLOBALS[$this->selected_module]) &&
is_object($GLOBALS[$this->selected_module]) &&
$GLOBALS[$this->selected_module]->enabled)
? $GLOBALS[$this->selected_module]->confirmation()
: null
Run Code Online (Sandbox Code Playgroud)
然而,对于三元运算符来说,这可以说是过于优雅,因此可读性可能是一个问题.