我有一个函数,如下面的代码所示,如果代码正确执行,它应该返回true
。什么新东西。
问题是函数调用其他函数,每个函数返回true
或false
。
我想弄清楚如何构建逻辑来验证函数的输出,检查函数本身的代码,以及其他函数的返回。其他函数可以调用,也可以不调用,这取决于$this->conf['functionName']
参数,该参数也是一个布尔值。
public function execute() {
$return = false;
if ($this->conf['functionOne']) {
$this->functionOne();
}
if ($this->conf['functionTwo']) {
$this->functionTwo();
}
if ($this->conf['functionThree']) {
$this->functionThree();
}
return $return;
}
我会这么做
public function execute() {
$function_list = ['functionOne', 'functionTwo', 'functionThree'];
$return = true;
foreach ($function_list as $function) {
if ($this->conf[$function]) {
if (!$this->{$function}() && $return) {
$return = false;
}
}
}
return $return;
}
我不太清楚,但我想你的意思是这样的…
public function execute() {
$res1 = $res2 = $res3 = true;
if($this->conf['functionOne']){
$res1 = $this->functionOne();
}
if($this->conf['functionTwo']){
$res2 = $this->functionTwo();
}
if($this->conf['functionThree']){
$res3 = $this->functionThree();
}
return ($res1 && $res2 && $res3);
}
你是这个意思吗?
public function execute() {
$return = false;
if($this->conf['functionOne']){
$return = $this->functionOne();
}
if($this->conf['functionTwo']){
$return = $this->functionTwo();
}
if($this->conf['functionThree']){
$return = $this->functionThree();
}
return $return;
}
可以吗?
public function execute() {
$f1 = $f2 = $f3 = false ;
if($this->conf['functionOne']){
$f1 = $this->functionOne();
}
if($this->conf['functionTwo']){
$f2 = $this->functionTwo();
}
if($this->conf['functionThree']){
$f3 = $this->functionThree();
}
if($f1=== true && $f2===true && $f3===true)
return true;
else
return false ;
}