Re: References for error handling methods
| From: | (Stig Sæther Bakken) | Date: | Thu, 17 May 2001 08:28:50 +0000 |
| Subject: | Re: References for error handling methods | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-145@lists.php.net to get a copy of this message | ||
[Matthew J Gray <matthew.j.gray@uwrf.edu>]
> Hello.
>
> I am a web applications programmer at a university
> and I think DB part of pear is great. We may be
> moving to other SQLs in the future and pear will
> more than likely make that transition much
> smoother...very cool.
>
> Anyways...
>
> I currently use error handling in a similar manner
> as pear, at least the function callback aspect of
> it. However, when I use methods of a class as the
> error callback, it is useful for me to pass them
> by reference because I have three different error
> modes for handling an error based on the app's
> production environment. When a method is
> performed on a referneced object, the current
> properties of the class(i.e. the error mode) are
> used rather than the defaults.
>
> The problem that I am encountering is that I
> cannot have my callback in pear be a referenced
> object and hence the default value is used as the
> error mode rather than a user defined value.
>
> consider the following:
>
> <?php
>
> class Test {
>
> var $mode =
> "DEFAULT";
>
>
> function SetMode() {
>
> $this->mode = "CHANGED";
> }
>
>
> function ErrorCallback() {
>
> echo $this->mode . "<br>";
> }
> }
>
>
> $test = new Test();
> $test->SetMode();
> $options = array( $test, "ErrorCallback" );
> $pear_db->setErrorHandling( PEAR_ERROR_CALLBACK,
> $options );
>
>
> ?>
>
> On error, the test class error function would
> return "DEFAULT" and not "CHANGED". In essence,
> another instance of the test object is being used
> for callback --not the one given to
> setErrorHandling. IMHO, that is greatly confusing
> and not practical. I have considered modifying
> PEAR.php, but messing with the $options array in
> setErrorHandling() seems like it would be a sticky
> endeavor.
>
> Did I make sense? and if so do you agree/disagree?
>
> Thank you for your time
You need to pass the object as a reference to avoid making a copy.
Try changing
$options = array($test, "ErrorCallback");
to
$options = array(&$test, "ErrorCallback");
and it should work.
- Stig
--
Stig Sæther Bakken <ssb@fast.no>
Fast Search & Transfer ASA, Trondheim, Norway