12-29-2007, 08:25 PM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
|
Quote:
Originally Posted by Tanax
Thanks for your input on my post :)
|
No problem.
Quote:
|
I don't really understand the whole php throw and get for errorhandling... maybe you know a good tutorial, or can write one yourself?
|
Sorry, for the delayed answer. I wrote a error handling class some time ago. I took one hour to figure out why the destructor wasn't called when I unset()'ted the instance of the class. 
I've commented that part in the code. If anyone has found a bug or has something to annotate please don't hesiate.  I'm really interested in your opinion about that topic. Here's the class:
PHP Code:
<?php
class ErrorHandler { //Todo: we can only use one ErrorHandler in the whole application as $functionStack is static, how do we solve this problem without //the need to make _exceptionHandler and _errorHandler non-static? private static $functionStack = array ( ) ; /** * Constructor * * @param mixed $function */ public function __construct ( $function = null ) { $className = get_class () ; //_exceptionHandler() and _errorHandler() must be static otherwise __destruct() is called in the end //so that we can't use unset() to restore the error- and exception handlers set_exception_handler ( array ( $className , '_exceptionHandler' ) ) ; set_error_handler ( array ( $className , '_errorHandler' ) ) ; if ($function !== null) { self::setErrorHandler ( $function ) ; } } private function __clone () { } /** * Destructor * */ public function __destruct () { restore_error_handler () ; restore_exception_handler () ; self::$functionStack = array ( ) ; } /** * Sets an error ahndler * * @param mixed $function */ public function setErrorHandler ( $function ) { if (is_callable ( $function )) { self::$functionStack [] = $function ; } } /** * Restores the error handler * */ public function restoreErrorHandler () { if (($key = count ( self::$functionStack ) - 1) >= 0) { unset ( self::$functionStack [ $key ] ) ; } } public static function _errorHandler ( $code , $message , $file , $line , $context , $trace = null ) { if (($key = count ( self::$functionStack ) - 1) >= 0) { if ($trace === null) { $trace = array_slice ( debug_backtrace (), 2 ) ; } $args = array ( $code , $message , $file , $line , $context , $trace ) ; call_user_func_array ( self::$functionStack [ $key ], $args ) ; return true ; } return false ; //use PHP's default error handler } public static function _exceptionHandler ( Exception $e ) { return self::_errorHandler ( $e->getCode (), $e->getMessage (), $e->getFile (), $e->getLine (), $GLOBALS, $e->getTrace () ) ; } }
Ok, so this is just a tutorial on how to use the class and will give you a little insight into the world of errors and exceptions. It will only cover the basics so please visit php.net. They have documented that topic pretty good.
PHP Code:
<?php
error_reporting ( E_ALL ) ;
function errorHandler ( $code , $message , $file , $line , $context , $trace ) { echo 'There was an error in line ' . $line . ' ("' . $message . '") - caught by errorHandler()' . PHP_EOL ; }
function errorHandler2 ( $code , $message , $file , $line , $context , $trace ) { echo 'There was an error in line ' . $line . ' ("' . $message . '") - caught by errorHandler2()' . PHP_EOL ; }
//initialize error handler $errorHandler = new ErrorHandler ( 'errorHandler' ) ;
//now the function errorHandler() should be called echo $notExistentVariable ;
//we can even let the error handler catch exceptions but the problem is everything after the exception won't be executed even though we've caught it so I've commented it out //throw new Exception('message');
//an exception is expected for example when a database class throws an exception because the SQL wasn't executed properly we can even prevent the error handler from catching it try { throw new Exception ( 'message' ) ; } catch ( Exception $e ) { echo 'Caught by try..catch' . PHP_EOL ; }
//it's also possible to 'nest' error handlers by adding one to the stack //the class will always use the error handler on top of the stack //so we'll use restoreErrorHandler() and we're using errorHandler() instead of errorHandler2() $errorHandler->setErrorHandler ( 'errorHandler2' ) ; echo $blah ; $errorHandler->restoreErrorHandler () ;
//okay, let's test if that works, now the error should be handled by errorHandler() echo $blah2 ;
//now destroy the error handler unset ( $errorHandler ) ;
//this warning should be handled by PHP's internal error handler echo $test ;
If everything's working fine you should get something like that:
Quote:
tim@tim-laptop:~/Desktop$ php errorHandler.php
There was an error in line 101 ("Undefined variable: notExistentVariable") - caught by errorHandler()
Caught by try..catch
There was an error in line 119 ("Undefined variable: blah") - caught by errorHandler2()
There was an error in line 123 ("Undefined variable: blah2") - caught by errorHandler()
Notice: Undefined variable: test in /home/tim/Desktop/errorHandler.php on line 130
|
Feedback is desired!
Last edited by deflated : 07-18-2010 at 01:39 PM.
|
|
|
|