我如何覆盖 Slim 的版本 2 默认错误处理函数

How I can override Slim's version 2 default error handling function

如何覆盖 Slim 的版本 2 默认错误处理?我不希望每次收到警告消息时我的应用程序都崩溃。基本上我想从 \Slim\Slim class.

覆盖函数 handleErrors()

我研究了如何覆盖此行为,但因为它被称为:

set_error_handler(array('\Slim\Slim', 'handleErrors')); 在 Sim 的 运行() 方法中,我不得不自己编辑 Slim 源代码。我将上面的内容更改为: set_error_handler(array(get_class($this), 'handleErrors')); 然后我用 handleErrors() 的不同行为扩展了 Slim 并实例化了我的自定义 class 而不是 Slim。它工作正常但我不想触及 Slim 的核心 class。 代码仅供参考

public static function handleErrors($errno, $errstr = '', $errfile = '', $errline = '')
{
    if (error_reporting() & $errno) {
        //Custom Block start here
        $search = 'Use of undefined constant';
        if(preg_match("/{$search}/i", $errstr)) {
            return true; //If undefined constant warning came will not throw exception
        }
        //Custom Block stop here
        throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
    }

    return true;
}

请帮助提供覆盖 handleErrors() 的正确方法

我在 index.php 中按以下方式处理它

// Prepare the Slim application container
$container = new Container();

// Set up error handlers
$container['errorHandler'] = function () {
    return new ErrorHandler();
};
$container['phpErrorHandler'] = function () {
    return new ErrorHandler();
};
$container['notAllowedHandler'] = function () {
    return new NotAllowedHandler();
};
$container['notFoundHandler'] = function () {
    return new NotFoundHandler();
};
// Set up the container
$app = new App($container);

$app->run();

也可以用这种方式覆盖默认的 slim 请求和响应

扩展 class 并覆盖各自的方法 handleErrors() 和 运行()。

class Core_Slim_Slim extends \Slim\Slim
{
public static function handleErrors($errno, $errstr = '', $errfile = '', $errline = '')
    {
        //Do whatever you want...
    }

public function run()
    {
        set_error_handler(array('Core_Slim_Slim', 'handleErrors'));
    }

}

实例化新的自定义 slim class 并调用其 运行 方法,如下所示。

$app = new Core_Slim_Slim();
$app->run();

感谢大家的回复:)