<?php
require_once 'PEAR.php';
// dummy error constant for this example
define('MYCLASS_ERROR_CODE', 1);
// demonstration of default global error handling
// in this case, all PEAR Errors will trigger a PHP warning
PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING);
// Note that the file and line number will be in the constructor of PEAR_Error
// in PEAR.php
PEAR::raiseError('test warning', MYCLASS_ERROR_CODE);
// this error specifies a mode, and overrides the default global error handling
$e = PEAR::raiseError('return only', MYCLASS_ERROR_CODE, PEAR_ERROR_RETURN);
PEAR::setErrorHandling(PEAR_ERROR_PRINT, "Gronk error: %s<br />\n");
// prints "Gronk error: test warning<br />\n"
PEAR::raiseError('test warning', MYCLASS_ERROR_CODE);
/**
* Fake class to demonstrate error handling
* @package myClass
*/
class myClass extends PEAR {
/**
* Demonstration of default local error handling
*/
function myClass()
{
// object method callback
$this->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'handleErr'));
// prints "custom handler...is working"
PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE);
// static class method callback
$this->setErrorHandling(PEAR_ERROR_CALLBACK,
array('myClass', 'handleErrStatic'));
PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE);
// function callback
$this->setErrorHandling(PEAR_ERROR_CALLBACK, 'standardCallback');
PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE);
}
/**
* Callback set by the constructor
* @param PEAR_Error The error object
*/
function handleErr($error)
{
$this->lastError = $error->getMessage();
print $error->getMessage() . "...is working\n";
}
/**
* Static callback set by the constructor
*
* Note that in PHP 5, $this is not set if the method is declared with
* the "static" access modifier. In PHP 4, $this is set, but is not
* set to the myClass object, so don't use it!
* @param PEAR_Error The error object
* @static
*/
function handleErrStatic($error)
{
print 'static ' . $error->getMessage() . "...is working\n";
}
}
/**
* @param PEAR_Error The error object
*/
function standardCallback($error)
{
print 'normal function callback: ' . $error->getMessage();
}
// This causes the printing of three messages through error callbacks:
// "custom handler...is working"
// "static custom handler... is working"
// "normal function callback: custom handler"
$mine = new myClass;
PEAR::setErrorHandling(PEAR_ERROR_DIE);
// terminates the script with the error message "oops"
PEAR::raiseError('oops', MYCLASS_ERROR_CODE);
?> |