note 97203 added to function.trigger-error
| From: | ray dot burgemeestre at _REPLACE_TO_G_mail dot com | Date: | Thu, 08 Apr 2010 09:15:27 +0000 |
| Subject: | note 97203 added to function.trigger-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168005@lists.php.net to get a copy of this message | ||
In reply to mano at easymail, I really like your idea. I wish I'd have seen it sooner, I then
might have actually used it on some PHP 4 projects I've been working on recently. The only
thing is that statements after the error-throwing-statement--inside the try-clause--should be
omitted. My proposal is adding an in_try() function.
I've converted your arrays to Exception objects, and made a few functions to make the code
"look" more like they are actually try-catch ;) They are probably more convenient to use
like this and they might even be easier to (mass)replace later on into PHP 5 code. The usage of
$GLOBALS could be replaced with a Singleton. To keep it short I omitted a few properties and
get**/set** methods in Exception.
<?php
class Exception
{
var $_message = NULL;
function Exception($message = '')
{
$this->_message = $message;
}
function getMessage()
{
return $this->_message;
}
}
class BadGirlException extends Exception
{
}
// Try-catch functions
function try()
{
$GLOBALS['exception'] = NULL;
}
function in_try()
{
return ($GLOBALS['exception'] === NULL);
}
function &catch(&$ex)
{
$ex = $GLOBALS['exception'];
return $ex;
}
function throw($exception)
{
$GLOBALS['exception'] = $exception;
trigger_error($exception->getMessage(), E_USER_NOTICE);
}
// Try-catch uses error handler to get filename, line number etc.
function try_catch_error_handler($errno, $errstr, $errfile, $errline)
{
if (is_object($GLOBALS['exception'])) {
// commented in this example
//$GLOBALS['exception']->setErrorNo($errno);
//(....)
// don't execute internal php error handler
return true;
} else {
// do execute internal php error handler
return false;
}
}
set_error_handler('try_catch_error_handler');
/**
* Badgirl function, get candy etc.
*
* @return boolean succesful execution
* @throws BadGirlException
*/
function badgirl()
{
$canHaveCandy = false;
if (!$canHaveCandy) {
return throw(new BadGirlException('Badgirls are not allowed to have candy'));
} else {
return true;
}
}
try(); {
if (in_try()) $sheis = badgirl();
if (in_try()) print "this line is not seen in case an exception occurred.\n";
} switch (get_class(catch($ex))) {
case 'badgirlexception':
printf("badgirl() throws exception: %s\n", $ex->getMessage());
print_r($ex);
break;
case 'exception':
default:
print_r($ex);
break;
}
print "end of program";
/** Program output (in case Exception::setErrorStr etc., have been implemented):
*
* badgirl() throws exception: Badgirls are not allowed to have candy
* badgirlexception Object
* (
* [_message] => Badgirls are not allowed to have candy
* [_errno] => 512
* [_errstr] => Badgirls are not allowed to have candy
* [_errfile] => /home/ray/test.php
* [_errline] => 68
* [_trace] => Array
* (
* [0] => Array
* (
* [function] => try_catch_error_handler
* [args] => Array
* (
* [0] => 512
* [1] => Badgirls are not allowed to have candy
* (.....)
*
* )
*
* )
* end of program
*/
?>
----
Server IP: 91.199.167.223
Probable Submitter: 81.18.253.210
----
Manual Page -- http://www.php.net/manual/en/function.trigger-error.php
Edit -- https://master.php.net/note/edit/97203
Del: integrated -- https://master.php.net/note/delete/97203/integrated
Del: useless -- https://master.php.net/note/delete/97203/useless
Del: bad code -- https://master.php.net/note/delete/97203/bad+code
Del: spam -- https://master.php.net/note/delete/97203/spam
Del: non-english -- https://master.php.net/note/delete/97203/non-english
Del: in docs -- https://master.php.net/note/delete/97203/in+docs
Del: other reasons-- https://master.php.net/note/delete/97203
Reject -- https://master.php.net/note/reject/97203
Search -- https://master.php.net/manage/user-notes.php