【发布时间】:2018-10-15 09:40:24
【问题描述】:
我有一个名为Error 的类,它在加载时将使用PHP 中的set_error_handler 方法注册一个名为errorHandler 的函数。但是,如果函数加载失败或 PHP 使用默认的内置错误处理程序,此函数将返回 NULL。我无法弄清楚为什么我的功能不被接受。有没有人有好的猜测?
<?php
declare (strict_types = 1);
namespace Request\Configuration;
use Errors\Exception\FatalException;
class Error extends Template
{
public static function load(): void
{
$result = set_error_handler('self::errorHandler', error_reporting());
var_dump($result);
}
public static function errorHandler(
int $number,
string $message,
string $file = null,
int $line = null,
array $context = null
) {
throw new FatalException('PHP_ERROR: ' . $message, 0);
}
}
这个类在加载时返回NULL。
【问题讨论】:
-
你为什么不使用 TRY - CATCH?
-
所有代码都包装在一个 catch 块中,因此当它冒泡时应该在上游捕获该异常。但为了确定,我删除了异常并做了一个 die() 语句,但它仍然没有工作:(
-
你可以尝试用 throw new Exception 替换 throw new FatalException 或者 throw new Error
-
我在 PHP 手册中找不到 FatalException 的帮助
-
@MangeshSathe 试了一下,仍然得到
NULL。不过想法不错。 FatalException 也只是我对 Exception 的扩展。
标签: php exception error-handling