[thelist] PHP: set_error_handler() and error suppression

Jack Timmons jorachim at gmail.com
Fri Mar 13 20:30:22 CDT 2009


On Fri, Mar 13, 2009 at 8:14 PM, Blake <haswell00 at gmail.com> wrote:

> Hi List,
>
> I'm a little confused by the behaviour I'm getting in a script. I'm
> writing a class to handle the errors in my app, but my error handler
> seems to get triggered even if I suppress an error. Relevant code:
>
> <?php
>        error_reporting(E_ALL);
>        ini_set('display_errors', 0);
>
>        set_error_handler("error::ErrorHandler");
>        set_exception_handler("error::ExceptionHandler");
> ?>
>
> <?php
>        /**
>         * Logs the error and redirects the user
>         *
>         * @param int $severity
>         * @param string $message
>         * @param string $filename
>         * @param int $line
>         */
>        public static function ErrorHandler($severity, $message, $filename,
> $line) {
>                self::$message =
>                        'Severity: ' . $severity
>                        . ' Message: ' . $message
>                        . ' File: ' . $filename
>                        . ' Line: ' . $line;
>
>                error_log(self::$message, 0);
>                self::Redirect();
>        }
>
>        /**
>         * Attempts to send a brief description of the exception via e-mail,
> then logs the exception and redirects the user
>         *
>         * @param Exception $e
>         */
>        public static function ExceptionHandler($e) {
>                @mail('email at address.com', 'ERROR: ' .
> $_SERVER['SERVER_NAME'],
> $e->getMessage(), 'From: email at address.com');
>                self::LogException($e);
>                self::Redirect();
>        }
> ?>
>
> I don't have a mail server on my local so mail() should fail, but it
> triggers an error even though I've used the error suppression
> operator. The error is:
>
> [Sat Mar 14 11:52:18 2009] [error] [client 127.0.0.1] Severity: 2
> Message: mail() [<a href='function.mail'>function.mail</a>]: Failed to
> connect to mailserver at &quot;localhost&quot; port 25, verify your
> &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use
> ini_set() File: ...\\error.php Line: 42
>
> Any ideas as to why this is happening?
>
> --
> Blake Haswell
> http://www.blakehaswell.com/
> --
>

This is a common question regarding set_error_handler.

And, luckily enough, it's right in the third paragraph of the documentation
[1]:

"It is important to remember that the standard PHP error handler is
completely bypassed."

The @ error suppressor is part of PHP error handler. They even make a note
saying, "Of particular note is that this value will be 0 if the statement
that caused the error was prepended by the @ error-control operator.".

1 - http://us2.php.net/manual/en/function.set-error-handler.php

-- 
-Jack Timmons
http://www.trotlc.com
Twitter: @codeacula



More information about the thelist mailing list