在Phil Sturgeon的REST控制器中为set_error_handler创建回调 [英] Creating a callback for set_error_handler in Phil Sturgeon's REST controller

查看:74
本文介绍了在Phil Sturgeon的REST控制器中为set_error_handler创建回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeIgniter中使用了Phil Sturgeon的REST_Controller,但我想像对我这样的老Java程序员更熟悉地使用try ... catch块。因此,我在Phil的控制器中添加了一个函数:

I'm using Phil Sturgeon's REST_Controller in CodeIgniter, but I would like to use try...catch blocks in a way more familiar to an old Java programmer like myself. So, I added a function to Phil's controller:

    function exception_error_handler($errno, $errstr, $errfile, $errline, $errcontext){
        $ee = new ErrorException($errstr, 0, $errno, $errfile, $errline, $previous);
        throw $ee;
    }

然后在 __ construct 控制器的方法,我试图通过错误处理将函数连接起来:

Then in the __construct method of the controller, I tried to connect that function up with error handling:

    set_error_handler("exception_error_handler");

当我现在运行扩展REST_Controller的控制器时,出现以下错误消息:

When I now run a controller that extends REST_Controller, I get this error message:


set_error_handler()期望
的参数(exception_error_handler)是有效的回调

set_error_handler() expects the argument (exception_error_handler) to be a valid callback

如果将函数和调用set_error_handler放在子类中,则不会发生错误,并且try ... catch块会在出现错误时捕获异常。为什么我的回调在超类中无效?

If I put the function and the call set_error_handler in the sub-class, there is no error and my try...catch blocks catch the exception whenever there is an error. Why isn't my callback valid in the super class?

推荐答案

我感谢user3345621为我指明了正确的方向。我将 set_error_handler 的PHP手册条目向下滚动到评论并找到了有关设置回调函数的三种方式的很好的建议。

I'm grateful to user3345621 for pointing me in the right direction. I scrolled down the PHP manual entry for set_error_handler to the comments and found a great piece of advice about 3 ways of setting a callback function.

在REST_Controller中,我添加了最初问题中提到的 exception_error_handler 函数,除了我将其静态设置为由用户3345621推荐。

In REST_Controller, I added the exception_error_handler function as mentioned in my original question, except that I made it static as recommended by user 3345621.

protected static function exception_error_handler($errno, 
                                                  $errstr, 
                                                  $errfile, 
                                                  $errline, 
                                                  $errcontext){
        $ee = new ErrorException($errstr, 0, $errno, $errfile, $errline, $previous);
        throw $ee;
    }

然后我注意到 early_checks 方法。我在 early_checks 方法中向set_error_handler添加了以下调用:

I then noticed the early_checks method in the REST_Controller. I added the following call to set_error_handler in the early_checks method:

set_error_handler(array(&$this, 'exception_error_handler'));

PHP手册中的注释说,&符很重要。

The comment in the PHP manual says that ampersand is important.

现在,当子类中发生错误时,将按我的要求抛出ErrorException。

Now when an error occurs in a sub-class, an ErrorException is thrown as I require.

这篇关于在Phil Sturgeon的REST控制器中为set_error_handler创建回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆