douhong9210 2018-10-15 09:40
浏览 150

为什么这个error_handler函数返回NULL?

I have a class called Error which when loaded will register a function called errorHandler using the set_error_handler method in PHP. However, this function will return NULL if the function fails to load or if PHP is using the default built-in error handler. I cannot figure out why my function is not being accepted. Does anyone have any good guesses?

<?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);
    }
}

This class when loads returns NULL.

  • 写回答

1条回答 默认 最新

  • douzuizhuo0587 2018-10-15 14:09
    关注

    Ok, after much debugging and exploration I have found the reason for this. The set_error_handler() function will only return the current error handler function before the time of registering a new one. It's a strange functionality. So if you want to find out if it was successfully registered you have to call this function twice. So when this function was returning NULL it was because the previously registered function was PHP's built-in one so it simply returned NULL. Watch out for this one, it's a weird function.

    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。