note 34526 added to function.trigger-error
| From: | mail at dooley dot cjb dot net | Date: | Wed, 30 Jul 2003 00:16:53 +0000 |
| Subject: | note 34526 added to function.trigger-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52976@lists.php.net to get a copy of this message | ||
i recently began using a custom error handling class. the biggest problem is that, with call time
pass by reference deprecated, you can't manipulate the error handler class after assigning at
as the error handler (and it appears not to be returned by the set_error_handler method as the old
error handler). my goal was to be able to store up all my non-fatal errors and print them at the end
of script execution. that way i can use 'trigger_error' (which i actually have wrapped in
a static method ErrorHandler::throwException for portability purposes... which is a pain because it
always has the same line number information!!) for all kinds of errors, including user input erros.
so when i check a user's password, for instance i would trigger a warning that said
'incorrect password'. of course i would only want this to print out the error once the
script had completed.
so in my error handler class i have the following in the constructor:
function ErrorHandler()
{
$this->error_messages = array();
error_reporting (E_ALL);
set_error_handler(array($this,"assignError"));
}
and my assignError method:
//accept the required arguments
function assignError($errno, $errstr, $errfile, $errline)
{
//get the error string
$error_message = $errstr;
//if in debug mode, add line number and file info
if(ErrorHandler::DEBUG())
$error_message .= "<br>".basename($errfile).",line: ".$errline;
switch ($errno)
{
//if the error was fatal, then add the error
//display an error page and exit
case ErrorHandler::FATAL():
$this->setType('Fatal');
$this->addError($error_message);
Display::errorPage($this->errorMessages());
exit(1);
break;
//if it was an error message, add a message of
//type error
case ErrorHandler::ERROR():
$this->setType('Error');
$this->addError($error_message);
break;
//if it was a warning, add a message of type
//warning
case ErrorHandler::WARNING():
$this->setType('Warning');
$this->addError($error_message);
break;
//if it was some other code then display all
//the error messages that were added
default:
Display::errorRows($this->errorMessages());
break;
}
//return a value so that the script will continue
//execution
return 1;
}
the key part there is the 'default' behaviour. i found that if i call trigger_error with
anything other than E_USER_ERROR, E_USER_WARNING or E_USER_NOTICE, then error code '2' is
passed to the handler method. so when it is time to print all my non-fatal errors, like
'password and confirm password don't match' or something, i call
ErrorHandler::printAllErrors()
function printAllErrors()
{
trigger_error("",2);
}
which leads to the default behaviour in my switch statement in the assignError method above. the
only problem with this is that the weird bug 'Problem with method call' that occurs with
some static method calls (that one person on the bug lists said was fixed and another said
wouldn't be fixed until version 5) also produces error code 2!! i have just taken to
suppressing these errors with @, because despite the alledged problem with the method call, the
script still seems to execute fine.
iain.
----
Manual Page -- http://www.php.net/manual/en/function.trigger-error.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34526
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34526&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34526&report=yes