Cannot access error stack in custom error handler
| From: | Carl Schmidt | Date: | Wed, 21 May 2003 00:02:52 +0000 |
| Subject: | Cannot access error stack in custom error handler | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148343@lists.php.net to get a copy of this message | ||
I'm attempting to create my own error handling code, consisting of an
ErrorManager class which contains an ErrorStack. (Yes this has been done
before, but I need the education myself). Whenever an error is added to
stack, I can see that it is present in the stack via debug statements in
ErrorManager and ErrorStack. However, the debug statement on test.php
indicates the stack is empty. Can anyone suggest what might be happening
here? Code snippets and output follow.
Code:
test.php
<snip>
//create the error manager, which contains the function that will handle
user errors
$errorManager=new ErrorManager();
echo "Adding Errors<BR>";
trigger_error("Here is a user Error",E_USER_ERROR);
echo "Error Count from test.php: " .
$errorManager->errorStack->getErrorCount() . "<BR>";
</snip>
ErrorManager.php
<snip>
var $errorStack;
function ErrorManager(){
$this->errorStack=new ErrorStack();
set_error_handler(array(&$this, 'handler'));
}
function handler($errno, $errmsg, $filename, $linenum, $vars) {
echo "Error Thrown <BR>";
$this->errorStack->addError($errno, $errmsg, $filename, $linenum,
$vars);
echo "Error Count from ErrorManager->handler(): " .
$this->errorStack->getErrorCount() . "<BR>";
}
</snip>
ErrorStack.php
<snip>
var $stack;
function ErrorStack(){
$this->stack=array();
}
//NOTE: do something with $vars
function addError($errorType,$errorMsg,$filename, $linenum, $vars){
array_push($this->stack,array($errorType,$errorMsg,$filename,
$linenum));
echo "Error Count from ErrorStack->addError(): " .
$this->getErrorCount() . "<BR>";
}
function getErrorCount(){
return count($this->stack);
}
</snip>
Output:
Adding Errors
Error Thrown
Error Count from ErrorStack->addError(): 1
Error Count from ErrorManager->handler(): 1
Error Count from test.php: 0