【问题标题】:call_user_func_array not break when it raise an exceptioncall_user_func_array 在引发异常时不会中断
【发布时间】:2023-03-06 01:55:02
【问题描述】:

这是代码(Laravel 中的一小部分):

foreach ($this->getListeners($event) as $listener) {

    $response = call_user_func_array($listener, $payload);

    Log::debug(var_export($response, true));

    ...
}

当我在call_user_func_array 周围添加try catch 块时,我可以捕获异常,但是当我删除它时,异常消失了,并且$response = TRUE; 我需要处理set_exception_handler 中的异常,谁能告诉我为什么?

注意:PHP 5.6,Laravel 5.2

【问题讨论】:

  • 你想 $response 是什么?如果有任何帮助,您可以简单地将 $response = false 添加到您的 catch 块中。
  • 它可能与app\Exceptions\Handler.php有关它有protected $dontReport = [ \Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\Access\AuthorizationException::class, \Symfony\Component\HttpKernel\Exception\HttpException::class, \Illuminate\Database\Eloquent\ModelNotFoundException::class, \Illuminate\Session\TokenMismatchException::class, \Illuminate\Validation\ValidationException::class, ];
  • 我试过了,但它不会到达那里,我可以在调用call_user_func_array之后获取日志,异常似乎消失了。
  • 当您删除 try catch 时,您确定异常真的“消失”(不会破坏您的代码)吗?可能var_export($response…只是从下一个入口输出$response这个代码,这是由一些外部代码引起的?
  • 尝试在整个 foreach 循环周围放置另一个 try...catch(以确保未在迭代本身中捕获的异常真正打破循环)。

标签: php laravel


【解决方案1】:

你应该能够在出错时自己破解它。

来自call_user_func_array docs

返回回调的返回值,错误时返回 FALSE。

要处理错误,您只需执行以下操作:

foreach ($this->getListeners($event) as $listener) {

    $response = call_user_func_array($listener, $payload);

    if($response === false) {
        // do something on error
        break;
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-07
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多