2

I can't work out what's going wrong.

The following always results in outputting "Could not set an error handler":

<?php

function die_on_error($errno, $errstr, $errfile, $errline, $errcontext){
    echo "\n\nThere was an error. Today is a good day to die.";
    echo "\n\n\$errno: $errno";
    echo "\n\n\$errstr: $errstr";
    echo "\n\n\$errfile: $errfile";
    echo "\n\n\$errline: $errline";
    echo "\n\n\$errcontext: ";
    var_dump($errcontext);
    die();
}

if (is_null(set_error_handler('die_on_error'))){
    die("\n\nCould not set an error handler.");
}
2
  • Are you actually generating an error? Commented Aug 11, 2015 at 4:04
  • try with ob_start('die_on_error'); Commented Aug 11, 2015 at 4:27

2 Answers 2

3

Because you need to have an error to execute this function,If errors occur before the script is executed the custom error handler cannot be called since it is not registered at that time.

function die_on_error($errno, $errstr, $errfile, $errline, $errcontext){
    echo"This is sparta";
    echo "\n\n\$errno: $errno";
    echo "\n\n\$errstr: $errstr";
    echo "\n\n\$errfile: $errfile";
    echo "\n\n\$errline: $errline";
    echo "\n\n\$errcontext: ";
    var_dump($errcontext);
    die();
}

 print_r(set_error_handler('die_on_error'));

trigger_error("Incorrect  array", E_USER_WARNING);

demo

Sign up to request clarification or add additional context in comments.

3 Comments

Great, it works. But I don't get it! Firstly, in both your code, die_on_error is defined before the call to set_error_handler. Secondly, in both cases, no error occurs before set_error_handler. confused
Aaah I was being really stupid. set_error_handler returns null even if you have successfully set a custom error handler, if the previous error handler was the PHP default. The only time it wouldn't return null is if you replace a custom error handler with something else
exactly ... you got it before my comment, nice R&D :D
0

I have a complicated singleton class for handling errors, but I also like to know that all has worked ok.

My solution was to silence error reporting and trigger an error with a specific string to check for then set a variable within the class to true and check against that.

something like

class myerror
{
    private $enabled;

    public function __construct()
    {
       if ($this->set_handler() === false) die('Error Handler Failed To Init...');
    }

    private function set_handler()
    {
        $methodVariable = array('myerror', 'error_handler');
        if (is_callable($methodVariable, true, $callable_name) === false return false;
        @set_error_handler($callable_name, E_ALL);
        trigger_error('Error Init', E_USER_NOTICE);
        return $this->enabled;
    }

    public static function error_handler($errNo, $errStr, $errFile, $errLine)
    {
        if($errStr == 'Error Init')
        {
            $this->enabled = true;
            return;
        }
        // do something with the error
    }
}

This is just a non tested example but I hope it gives you some idea how I solved the problem. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.