-1

It's my understanding that register_shutdown_function is a 'valid' manner in which to catch top level errors in PHP, as noted here.

Now - I've got a rather full error handling implementation that I hope to force to catch EVERY error that PHP generates after initialization. My code to do so looks something like this:

// 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"]);

For the sake of being thorough; here are my globals as defined:

// Config.php - Required from global scope.
$ErrorCallback = "HandleRuntimeError";
$ExceptionCallback = "HandleException";
$FatalCallback = "HandleFatalError";

$EnableReporting = true;

$ErrorLevel = E_ALL;

And furthermore; my actual methods meant to catch said errors:

// 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']);
}

Now - the way I see it at this point I should be getting effectively no output in the event of an error ( since my 'handlers' are empty ); but I am in fact still getting 'standard' reporting - the example being:

Fatal error: require(): Failed opening required '***/resource/php/startup/IncludeHandler.php' (include_path='.;C:\php\pear') in ***\index.php on line 18

Which; again for the sake of being thorough - looks something like this:

// index.php
function Initialize()
{   
    require($GLOBALS["PHPRoot"] . "errors/ErrorHandling.php");
    require($GLOBALS["PHPRoot"] . "errors/ErrorInitialization.php");
    require($GLOBALS["PHPRoot"] . "startup/IncludeHandler.php"); // Line 18
}

Moral of the story: Does anyone have a quick answer as to why the 'HandleFatalError' method isn't getting called?

-- Am I screwing myself over by turning display_errors on?

Community
  • 1
  • 1
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • Well that's just rude... A vote to close? Thanks a lot... – DigitalJedi805 Nov 25 '14 at 17:00
  • Lol I'm in shock - a close vote and a downvote becaaaaaaause.... why? Is my question not well formed, with hard examples and explanation? Good thing I answered my own fething question. Some marvelous community you all make here. Always some BS with the entitled 'wannamods' at SO... Infuriating TRASH. It's a complete question that I put research into and if I had to ask it - someone else is going to be curious sooner or later. I'd start throwing insults at the offending parties now but I'm running out of characters. – DigitalJedi805 Nov 25 '14 at 22:07

1 Answers1

0

If the INI configuration options 'display_errors' or 'display_startup_errors' are set to 1 ( or true ), the errors will still be displayed, regardless of the presence of a method to handle the errors.

In this case, my workaround came out something like this:

Edit To Add for Globals

// 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);
}   
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41