【问题标题】:set_exception_handler is not working with PHP CLI?set_exception_handler 不适用于 PHP CLI?
【发布时间】:2014-06-09 15:41:55
【问题描述】:

我是否遗漏了 php 文档中的某些内容?

我在我的index.php 上设置了一个set_exception_handler 以捕获异常(来自库)并避免未捕获异常的 PHP 致命错误。

当我使用 Apache 运行我的脚本时,我没有问题(捕获异常的函数可以正常工作)。 但是当我尝试使用 PHP cli (php -r 'include "index.php";') 时,我得到了 PHP 致命错误。

有什么想法吗?

-- index.php

set_exception_handler('exception_handler');
throw new Exception("Test exception");

function exception_handler($e) {
    echo '-- excemption handler --';
    $toLog = '['.date('d/m/Y H:i:s').'] '.$e->getMessage()."\n".$e->getTraceAsString();
    var_dump($toLog);
}

【问题讨论】:

  • 展示你的代码怎么样?
  • 这是一个非常奇怪的问题,它会咬那些偶尔使用php -r 来测试sn-p 的人。看起来像一个错误。

标签: php exception exception-handling command-line-interface


【解决方案1】:

要运行你的代码,你应该使用

php index.php

这不会显示致命错误。

当你跑步时

php -r '包括“index.php”;'

它实际上将语句作为 php 脚本执行。而且这个脚本本身不会处理自己的致命错误。

【讨论】:

  • 是的,没有更多错误,但在我的脚本中,我需要将 var 传递给 index.php...这就是我使用 php -r 的原因。我可以尝试用其他方式传递我的 var(例如 php index.php args)。
猜你喜欢
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 2014-12-06
  • 2016-03-06
  • 2016-12-31
  • 2019-07-09
  • 2014-10-26
  • 1970-01-01
相关资源
最近更新 更多