Re: References for error handling methods
| From: | Matthew J Gray | Date: | Fri, 18 May 2001 20:45:04 +0000 |
| Subject: | Re: References for error handling methods | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-148@lists.php.net to get a copy of this message | ||
Stig Sæther Bakken wrote:
>
> [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
>
> --
> PEAR General Mailing List (http://pear.php.net/)
> To unsubscribe, e-mail: pear-general-unsubscribe@lists.php.net
> For additional commands, e-mail: pear-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
Thanks for your help, but I think maybe the
problem I am having
might be deeper than pear. I had tried the method
you suggested
(passing &$options rather than $options) prior to
writing in
about my problem. I'm no expert when it comes to
references,
but in other situations passing references worked
for me and was
surprised when passing a reference didn't work in
this case.
I simplified the first example that I wrote. In
my app,
setErrorHandling was actally being called from
within
another class, similar to the following example.
<?php
class Test {
var $mode = "DEFAULT";
var $db;
/* Constructor */
function Person() {
$this->ConnectDB();
}
function ConnectDB() {
$options = array( &$this, 'ErrorHandler' );
$this->db = DB::connect($dsn );
$this->db->setErrorHandling(
PEAR_ERROR_CALLBACK, $options );
if( PEAR::isError( $this->db ) )
echo ( "Connection Failed" );
else
echo ( "Connection Succesful<br>" );
}
function causeDBError() {
$res = $this->db->query( $bad_query );
}
function SetMode( $mode = null ) {
$this->mode = "CHANGED";
}
function ErrorHandler( ) {
if( $this->mode == "DEFAULT" )
echo ( "DEFAULT Error message" );
else
echo ( "CHANGED Error Message" );
}
}
Nothing special there, but here's the tricky part-
If I then type the code:
<?php
$test = new test();
$test->SetMode();
$test->causeDBError();
?>
The resulting output is "DEFAULT Error message"
(not what I would expect).
However, if I remove the constructor from the
class and explicitly call
ConnectDB() externally:
<?php
$test = new Person();
$test->ConnectDB();
$test->SetMode();
$test->causeDBError();
?>
The resulting output is "CHANGED Error message"
(what I expect).
So as far as I can tell, the presence of a
constructor somehow
effects the referenced object that is passed to
setErrorHandling().
Strange... If this is a problem, it seems like a
PHP problem rather
than a PEAR one.
Thank you again for your help. Any further help
would be
greatly appreciated.
Matthew J. Gray
University of Wisconsin River Falls
Information Technology Services
matthew.j.gray@uwrf.edu