2

我有一个类似于以下的代码,但它不起作用。

class Abc extends CI_Controller {

    static function exception_handler(Exception $ex)
    {       
        var_dump($ex);
        echo $ex->getMessage();
        exit;
    }

    function __construct()
    {
        parent::__construct();
        set_exception_handler('exception_handler');     
    }       

    function Index()
    {

        throw new Exception('hehe');
    }
}   

我明白了

遇到 PHP 错误

严重性:警告

消息:set_exception_handler() 期望参数 (exception_handler) 是一个有效的回调

如何在 codeigniter 中使用 set_exception_handler

4

4 回答 4

3

Set_exception_handler 总是需要全名类,包括命名空间,并省略“使用指令”以使用其他命名空间。

在您的情况下,全名是:'CI_Controller:exception_handler'

因此正确的调用是: set_exception_handler('CI_Controller:exception_handler');

如果您有一个命名空间,例如命名空间 App;

正确的调用是: set_exception_handler('App\CI_Controller:exception_handler');

于 2012-10-29T11:36:47.300 回答
3

既然exception_handler是类中的一个函数,它应该是:

set_exception_handler(array('self','exception_handler')); 

或者

set_exception_handler(array('Abc','exception_handler'));

于 2012-06-24T13:46:20.737 回答
1
function Index()
{
    function ExceptionHandler($e) use($this)
    {
        $this->load->view('error', $this->sharedData);
    }

    set_exception_handler('ExceptionHandler');

    throw new Exception('hehe');
}
于 2014-03-31T14:41:48.773 回答
1

您应该使用访问级别为公共的实例化对象的方法。


final class MyException
{
    /**
     * Constructor
     * trigger getErrorTypesFromDefinedConstant method
     */
    public function __construct()
    {
        // Set global exception handler
        set_exception_handler(array($this, 'globalException'));
    }

    public function globalException($e)
    {
        // Your code to handle exception
    }
}

于 2016-10-20T07:59:24.413 回答