我的理解是register_shutdown_function是一个有效的'如here所述,在PHP中捕获顶级错误的方式。
现在 - 我有一个相当完整的错误处理实现,我希望强制捕获PHP在初始化后生成的每个错误。我这样做的代码看起来像这样:
// ErrorInitialization.php
if($GLOBALS["EnableReporting"])
error_reporting($GLOBALS["ErrorLevel"]);
else
error_reporting(0);
ini_set('display_errors',$GLOBALS["EnableReporting"]);
ini_set('display_startup_errors',$GLOBALS["EnableReporting"]);
set_error_handler($GLOBALS["ErrorCallback"]);
set_exception_handler($GLOBALS["ExceptionCallback"]);
register_shutdown_function($GLOBALS["FatalCallback"]);
为了彻底;这是我定义的全局变量:
// Config.php - Required from global scope.
$ErrorCallback = "HandleRuntimeError";
$ExceptionCallback = "HandleException";
$FatalCallback = "HandleFatalError";
$EnableReporting = true;
$ErrorLevel = E_ALL;
此外;我的实际方法意味着捕获所述错误:
// ErrorHandling.php
function HandleRuntimeError($ErrorLevel,$ErrorMessage,$ErrorFile,$ErrorLine,$ErrorContext)
{
}
function HandleException($Exception)
{
}
function HandleFatalError()
{
$Error = error_get_last();
// Pass fatal errors up to the standard error callback.
$GLOBALS["ErrorCallback"]($Error['type'],$Error['message'],$Error['file'],$Error['line']);
}
现在 - 我在这一点上看到它的方式,如果发生错误,我应该有效地获得输出(因为我的处理程序是空的);但事实上我仍然没有达到“标准”的标准。报告 - 例如:
Fatal error: require(): Failed opening required '***/resource/php/startup/IncludeHandler.php' (include_path='.;C:\php\pear') in ***\index.php on line 18
哪些;再次为了彻底 - 看起来像这样:
// index.php
function Initialize()
{
require($GLOBALS["PHPRoot"] . "errors/ErrorHandling.php");
require($GLOBALS["PHPRoot"] . "errors/ErrorInitialization.php");
require($GLOBALS["PHPRoot"] . "startup/IncludeHandler.php"); // Line 18
}
故事的道德:有没有人能快速回答为什么' HandleFatalError'方法没有被调用?
- 我是否通过打开display_errors来搞砸自己?
答案 0 :(得分:0)
如果INI配置选项' display_errors'或者' display_startup_errors'设置为1(或为真),无论是否存在处理错误的方法,错误仍将显示。
在这种情况下,我的解决方法是这样的:
编辑添加全局
// config.php
// Name of your error callback method
$ErrorCallback = "HandleRuntimeError";
// Name of your exception callback method
$ExceptionCallback = "HandleException";
// Name of your 'shutdown' (fatal) callback method
$FatalCallback = "HandleFatalError";
// Whether or not to enable error reporting at all. Should be overridden by a 'debugging' cookie or some such probably.
$EnableReporting = true;
// Level Options:
// -1 :: Report all PHP errors (( Much like E_ALL ) regardless of future changes to the following flags )
// 0 :: Disable All Reporting ( alternately handled using the above boolean )
// E_ALL :: All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0.
// E_STRICT :: Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
// E_DEPRECATED :: Run-time notices. Enable this to receive warnings about code that will not work in future versions.
// E_ERROR :: Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
// E_NOTICE :: Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
// E_PARSE :: Compile-time parse errors. Parse errors should only be generated by the parser.
// E_WARNING :: Run-time warnings (non-fatal errors). Execution of the script is not halted.
// E_CORE_ERROR :: Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
// E_CORE_WARNING :: Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
// E_COMPILE_ERROR :: Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
// E_COMPILE_WARNING :: Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
// E_USER_ERROR :: User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_NOTICE :: User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_WARNING :: User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_DEPRECATED :: User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
// Error reporting level
// Example: $ErrorLevel = E_PARSE | E_STRICT;
$ErrorLevel = E_ALL;
// ErrorInitialization.php
if($GLOBALS["EnableReporting"])
{
error_reporting($GLOBALS["ErrorLevel"]);
if( isset($GLOBALS["ErrorCallback"]) && strlen($GLOBALS["ErrorCallback"]) > 0 )
{
$Callback = $GLOBALS['ErrorCallback'];
set_error_handler($GLOBALS["ErrorCallback"]);
ini_set('display_errors',false);
}
else
ini_set('display_errors',$GLOBALS["EnableReporting"]);
if( isset($GLOBALS["FatalCallback"]) && strlen($GLOBALS["FatalCallback"]) > 0 )
{
$Callback = $GLOBALS['FatalCallback'];
register_shutdown_function($GLOBALS["FatalCallback"]);
ini_set('display_startup_errors',false);
}
else
ini_set('display_startup_errors',$GLOBALS["EnableReporting"]);
if( isset($GLOBALS['ExceptionCallback']) && strlen($GLOBALS['ExceptionCallback']) > 0 )
set_exception_handler($GLOBALS["ExceptionCallback"]);
}
else
{
ini_set('display_errors',0);
ini_set('display_startup_errors',0);
error_reporting(0);
}