I've been recoding a fairly large project with main aim of making it completely object oriented.

I have made a custom error handler class with a member function to handle errors and a constructor to set the member function as the error handler. Something like:-

class error
{
    function error
    {
        set_error_handler(array(&$this, 'error_handler')
    }

function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
    // Code to execute on error.
}
}

$error = new error;

trigger_error('My error!');

Using a member function as the error handler works fine in PHP >= 4.3.0 as it should do according to the docs, but older versions of PHP do not allow for array of object and method to be used as set_error_handler() parameter. Is there any way around this without moving my error handler functions outside of any class?

    either upgrade
    or do

    global $error;
    $error->error_handler($errno, $errstr, $errfile, $errline, $errcontext);

    in the error handler outside of the class

      Thanks for the suggestion Mr Happiness.

      I was really hoping there may be a way of avoiding using a seperate error handling function outside of the error class, even if it does end up calling my class's member function.

      It's not a big problem (if a problem at all!) but it's just one of those things that bugs me a little. I'm developing the app for countless users, running different servers, etc, so want to avoid anything specific to PHP >= 4.3.0.

      I'll make do with using a function outside of the class for now as it seems the closest to what I want, but if there is a workaround that lets me specify class member function directly with set_error_handler() I'd love to know what it is (PHP < 4.3.0 of course).

        Write a Reply...