Re: Error gettig automatically raised ?
| From: | (Stig Sæther Bakken) | Date: | Thu, 23 Aug 2001 13:19:53 +0000 |
| Subject: | Re: Error gettig automatically raised ? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-284@lists.php.net to get a copy of this message | ||
[Markus Fischer <mfischer@guru.josefine.at>]
> Hi,
>
> When I define PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, ..) how
> can I make this callback automatically called when an error
> occurs ?
>
> e.g, my class does
>
> class foo extends PEAR {
>
> function foo() {
>
> $this->PEAR();
>
> PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this,'method'));
>
> // that is fine
> if ($some_test)
> $this->raiseError('foobar corrupted', PEAR_ERROR_CALLBACK);
>
> $this->_db = DB::connect( long dsn ...
>
> // now shouldn't the connect automatically raise an error
> // ?
> [..snipped...]
>
> I use PEAR::setError and not $this->setError because I want all
> errors be handled by this class...
Setting the global error handler in a class constructor seems weird to
me, I wouldn't design my code that way. In general, you should avoid
doing stuff that can result in PEAR errors from a constructor, because
you have no way of giving the error object back to the caller.
I suggest you make a simpler constructor, move the DB stuff to a new
method and set up the error handler after new'ing the object:
$obj = new foo;
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array(&$obj, 'method'));
$obj->setup();
I assume the reason you want to set the global handler is that you
want your _db object to use the same error handler as well? I've had
the same problem myself, and I'm trying to think of a way to let
aggregate objects "inherit" error reporting parameters.
I'll drink to exceptions anytime. :-)
- Stig
--
Stig Sæther Bakken <ssb@alltheweb.com>
Fast Search & Transfer ASA, Trondheim, Norway