For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2005 > [PEAR] How to use PHP 4 classes with exceptions?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [PEAR] How to use PHP 4 classes with exceptions?
Sven Drieling

2005-05-26, 8:59 pm

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_EXCEPT
ION) 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_CALLBA
CK, 'pearErrorHandler');

function pearErrorHandler($error)
{
throw new Exception($error->getMessage() . ' -- ' . $error->getDebugInfo());
}

but using PEAR::setErrorHandling(PEAR_ERROR_EXCEPT
ION) would be easier.

So what is the best way to use PEAR classes written for PHP 4 with exceptions?


tschuess
[|8:)
Greg Beaver

2005-05-30, 8:57 pm

Sven Drieling wrote:
> So at the moment I'm just using a PEAR error handler to throw exceptions
>
> PEAR::setErrorHandling(PEAR_ERROR_CALLBA
CK, 'pearErrorHandler');
>
> function pearErrorHandler($error)
> {
> throw new Exception($error->getMessage() . ' -- ' . $error->getDebugInfo());
> }
>
> but using PEAR::setErrorHandling(PEAR_ERROR_EXCEPT
ION) would be easier.


With a difference of 3 extra lines, I don't see how this is difficult,
less efficient, or inappropriate. I think your solution is the best one
and shows the true flexibility of the PEAR_ERROR_CALLBACK and callbacks
in general.

Greg
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com