Re: DB: do I need to check if (DB::isError($res)) if I setErrorHandling() to callback function?
| From: | Justin Patrin | Date: | Wed, 24 Aug 2005 16:45:43 +0000 |
| Subject: | Re: DB: do I need to check if (DB::isError($res)) if I setErrorHandling() to callback function? | ||
| References: | 1 2 3 4 5 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20578@lists.php.net to get a copy of this message | ||
On 8/24/05, Massimiliano Arione <garak@studenti.it> wrote:
> Justin Patrin wrote:
> > If you use the callback but don't exit the script in some way then you
> > *are* going to have errors in your scripts. Likely "method does not
> > exist" errors. If you do logging and such and *then* die() or exit
> > you're still fine.
>
> But even that errors will be catched by the callback.
> Or not?
>
No. The errors you will get will be PHP errors, not PEAR errors. Take
this for example:
require_once('DB.php');
function handleError($err) {
echo $err->getMessage();
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
$db =& DB::connect('a bad DSN');
$sth =& $db->query('SELECT * FROM table');
There will be a DB connection error because the DSN is bad and the
message will be output. However, since the callback doesn't end the
script the query() line will still be run. Since $db is now a
PEAR_Error and not a DB object you're going to get an undefined method
error. This is a PHP error, not a PEAR error and it will stop
execution of yoru script anyway. If you're only using a callback for
error handling (not handling errors from returns) then you need to put
a die() or exit in the callback to avoid this kind of error.
--
Justin Patrin