How to use PHP 4 classes with exceptions?
| From: | Sven Drieling | Date: | Fri, 20 May 2005 16:10:30 +0000 |
| Subject: | How to use PHP 4 classes with exceptions? | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-19310@lists.php.net to get a copy of this message | ||
Hello,
PHP 5.04, PEAR 1.4.0a11
what is the best way to use PEAR classes written for PHP 4 with exceptions to
write code like
<?php
require_once 'PEAR.php';
require_once 'DB.php';
try {
$db = DB::connect('pgsql://usr:pw@localhost/dbnam');
// Proceed with a query...
$res = $db->query('SELECT * FROM clients');
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
Using PEAR::setErrorHandling(PEAR_ERROR_EXCEPTION) returns
a warning
Warning: PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_ErrorStack for exceptions
in ...php-5.0.4/lib/php/PEAR.php on line 890
but the PEAR_ErrorStack does not support exception without an own callback
* Since version PEAR1.3.2, ErrorStack no longer instantiates an exception class. This can
* still be done quite handily in an error callback or by manipulating the returned array
So at the moment I'm just using a PEAR error handler to throw exceptions
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pearErrorHandler');
function pearErrorHandler($error)
{
throw new Exception($error->getMessage() . ' -- ' . $error->getDebugInfo());
}
but using PEAR::setErrorHandling(PEAR_ERROR_EXCEPTION) would be easier.
So what is the best way to use PEAR classes written for PHP 4 with exceptions?
tschuess
[|8:)