gpt4 book ai didi

php - Zend 框架中的 register_shutdown_function

转载 作者:行者123 更新时间:2023-12-03 08:08:13 24 4
gpt4 key购买 nike

我试图捕捉 Zend Framework 应用程序中发生的 fatal error 。我试图从 Bootstrap 中调用 register_shutdown_function:

protected function _initErrorHandler()
{
register_shutdown_function(array("My_ErrorHandler", 'shutdownHandler'));
}

然后,在 My_ErrorHandler 中,我编写了一个函数:
<?php
class My_ErrorHandler
{
static function shutdownHandler()
{
die('here');
}
}

这行不通。我对 set_error_handler 进行了同样的尝试,它可以工作。但是,最后一个函数无法捕获 fatal error 。

你是我想念的吗?

谢谢

最佳答案

我设法使它工作。

事实上,它是有效的,但这不是很“友好”。 die('here') 被击中,但在浏览器中显示 fatal error 之后。

所以我发现了 fatal error ,然后重定向到标准 Zend Framework 错误 Controller ,结果如下:

<?php
class My_ErrorHandler
{
/**
* Catch all errors within the Applications
* @see Bootstrap
*/
static function shutdownHandler()
{
$e = error_get_last();
if (!is_null($e) && $e['type'] == E_ERROR) { //fatal error

//log
Zend_Registry::get('log')->err($e['message']);

//Redirect to error page
$redirector = new Zend_Controller_Action_Helper_Redirector();
$redirector->gotoSimple('error', 'error', null, array('error_handler' => 'fatal'));
}
}
}

希望它会帮助别人;)

感谢@Jon、@Richard Parnaby-King 和@ficuscr 的帮助。

注意:尽量保持shutdownHandler() 尽可能轻:它被称为每个页面显示。

关于php - Zend 框架中的 register_shutdown_function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com