Using a custom error class

Whenever you call any of the raiseError()}}, {{{raiseWarning(), raiseNotice() or raise() methods, they will create a new instance of the patError class and return it.

In some cases, you may want to replace this replace this class with your own class that provides some additional functionality. Your custom error class should always extend the patError class. Here is an example of a class that provides some additional functionality, by automatically logging every error:

<?php
/**
 * Custom error class that writes triggered errors
 * to the file /var/log/pat-errors.log
 */
class MyError extends patError {
    
    function MyError( $level, $code, $msg, $info = null ) {
        parent::__construct( $level, $code, $msg, $info );
        $this->_writeErrorToFile();
    }

    function _writeErrorToFile() {
        $string = implode('|', array(
                                 patErrorManager::translateErrorLevel($this->level),
                                 $this->code,
                                 $this->message));
        error_log($string, 3, '/var/log/pat-error.log');
    }
}
?>

Now we need to tell patErrorManager to use this class instead of patError, when a new error is triggered. This can be easily achieved, using the setErrorClass() method of patErrorManager:

<?php
require_once 'pat/patErrorManager.php';
require_once 'MyError.php';

patErrorManager::setErrorClass('MyError');

patErrorManager::raiseError(E_ERROR, 1, 'This is only a test error.');
?>

Now you should see a new entry in the file /var/log/pat-errors.log. Of course, this functionaly could also be accomplished by using a custom error handler, but it was only an example what can be done using a custom class.