[PHP] Error Handling within Objects

Associate
Joined
18 Oct 2002
Posts
2,055
Location
Brighton, UK
I have a funny problem that I can't seem to get arround, when you have a error handling function within a class, registered as
Code:
set_error_handler(array($this, "handleerror"));
Any assignments that error handler makes to the object through $this->variable don't work. Anyone got any ideas?

Code:
PHP:
<?php
 $testObject = new rsObject("Passed");
 print_r($testObject);
?>

<?php
class rsObject
{
  var $sTextFromConstructor;
  var $sTextFromMethod;
  var $sTextFromErrorHandler;

  function rsObject($sInput)
  {
    echo ("Constructor was called\n");
    $this->sTextFromConstructor = $sInput;
    $this->updatetext($sInput);

    set_error_handler(array($this, "handleerror"));
    mysql_connect("server", "username", "password");
    restore_error_handler();
  }

  function updatetext($sUpdate)
  {
    echo ("Updatetext was called\n");
    $this->sTextFromMethod = $sUpdate;
  }

  function handleerror($errno, $errstr, $errfile, $errline)
  {
    echo ("Handleerror was called\n\n");
    $this->sTextFromErrorHandler = "Passed";
  }
}
?>

Output
Code:
Constructor was called
Updatetext was called
Handleerror was called

rsobject Object
(
    [sTextFromConstructor] => Passed
    [sTextFromMethod] => Passed
    [sTextFromErrorHandler] => 
)
 
robmiller said:
Works fine with PHP5 here, too.

Tried passing $this by reference? i.e:

PHP:
<?php
set_error_handler(array(&$this, "handleerror"));
?>

Unfortunatly PHP5 is not open to me at this point, if it were I would ditch this set_error_handler stuff in favor of try...catch blocks.

However passing it by reference worked great... didn't know how to do that... thanks rob.
 
Back
Top Bottom