来自内部函数调用的结束函数-PHP

来自内部函数调用的结束函数-PHP,php,laravel,lumen,Php,Laravel,Lumen,我有一个需要多次检查的函数,为此,我添加了多个函数,但当某个内部函数失败时,它需要返回失败的响应,但它不会返回,并继续下一个内部函数 public static function doMultipleWorks(){ self::checkFirstCondition(); self::checkSecondCondition(); ... ... return response(['status' => true, 'data' => [...]]);

我有一个需要多次检查的函数,为此,我添加了多个函数,但当某个内部函数失败时,它需要返回失败的响应,但它不会返回,并继续下一个内部函数

 public static function doMultipleWorks(){

  self::checkFirstCondition();
  self::checkSecondCondition();

  ...
  ...

  return response(['status' => true, 'data' => [...]]);

 }

 public static function checkFirstCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }

 }

 public static function checkSecondCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }


 }

问题是,如果第一个或第二个函数失败,它仍然会继续,并且不会中断该函数。非常感谢您的帮助。

您没有检查
checkFirst
checkSecond
的返回值,请执行此操作或抛出异常以中断函数并尝试/捕获异常

public function foo() {
     if ($bar = $this->bar()) return $bar;
}

public function bar() {
   if (something) return resp;
}


您没有检查
checkFirst
checkSecond
的返回值,执行此操作或引发异常以中断函数并
try/catch
异常

public function foo() {
     if ($bar = $this->bar()) return $bar;
}

public function bar() {
   if (something) return resp;
}


您需要检查函数的响应,在响应的基础上,您应该继续或中断进一步的进程。我认为你应该这样做:

public static function doMultipleWorks(){

  $firstResponse = self::checkFirstCondition();
  if ($firstResponse['status'] == false) {
       return $firstResponse;
  }
  $secondResponse = self::checkSecondCondition();
  if ($secondResponse['status'] == false) {
       return $secondResponse;
  }

  ...
  ...

  return response(['status' => true, 'data' => [...]]);

 }

 public static function checkFirstCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }

 }

 public static function checkSecondCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }


 }

希望它能帮助您解决问题。

您需要检查函数的响应,并在响应的基础上,继续或中断进一步的过程。我认为你应该这样做:

public static function doMultipleWorks(){

  $firstResponse = self::checkFirstCondition();
  if ($firstResponse['status'] == false) {
       return $firstResponse;
  }
  $secondResponse = self::checkSecondCondition();
  if ($secondResponse['status'] == false) {
       return $secondResponse;
  }

  ...
  ...

  return response(['status' => true, 'data' => [...]]);

 }

 public static function checkFirstCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }

 }

 public static function checkSecondCondition(){

  ....
  ....
  if(this != that){
    return response(['status' => false, 'error_msg' => 'this is not equal to that']]
  }


 }
希望它能帮助你修正你的方法