<?php
 
/**
 
* This script illustrates how to use function "raiseError()".
 
*/
 
 
// Set the search path.
 
ini_set('include_path', $_SERVER['DOCUMENT_ROOT'].'/PEAR');
 
 
require_once('Error.php');
 
 
// it is possible to use directly in the script.
 
// after call, raises warning in line 13
 
raiseError('Test error, raises directly in the script', E_USER_WARNING);
 
 
// it is possible to use insidethe function.
 
function Simple()
 
{
 
    raiseError('Test error, raises inside the function', E_USER_WARNING);
 
}
 
 
// after call function, raises warning inline 22.
 
Simple();
 
 
// it is possible to use inside a class with intermediate function.
 
class SimpleClass
 
{
 
    function SimpleClass()
 
    {
 
        $this->SimpleException();
 
    }
 
    
 
    function SimpleException()
 
    {
 
        /**
 
        * The constructor of a class uses this method for check. That the debug
 
        * information was displayed not for the constructor, and for function
 
        * calling it, we specify callLevel = 3. If method called directly, we
 
        * specify callLevel = CLASS_LEVEL.
 
        */
 
        $callLevel = (getCallerMethod() == 'simpleclass') ? 3 : CLASS_LEVEL;
 
        $this->raiseError('Test Exception', E_USER_WARNING, $callLevel);
 
    }
 
    
 
    /**
 
    * That the debug information was displayed for caller function, the
 
    * parameter $callLevel is specified CLASS_LEVEL.
 
    */
 
    function raiseError($message, $type, $callLevel = CLASS_LEVEL)
 
    {
 
        $message = 'SimpleClass error: '.$message;
 
        raiseError($message, $type, $callLevel);
 
    }
 
}
 
 
// after call constructor, raises warning in line 56.
 
$simple = new SimpleClass;
 
 
// after call method, raises warning in line 59.
 
$simple->SimpleException();
 
 
// it is possible to use user-defined error handler.
 
function handleError($message, $type, $file, $line)
 
{
 
    print '<br>Called user-defined error handler';
 
    exit("<br>Error: $message in $file on line $line");
 
}
 
 
// Other example of error handling with use of class PEAR.
 
class PEAR_Class extends PEAR
 
{
 
    function PEAR_Class()
 
    {
 
        $this->PEAR('Class_Error');
 
    }
 
    
 
    function SimpleException()
 
    {
 
        $this->raiseError('Test Exception');
 
    }
 
    
 
    function runSimpleException()
 
    {
 
        $this->SimpleException();
 
    }
 
}
 
 
class Class_Error extends Error
 
{
 
    var $error_message_prefix = 'PEAR_Class error: ';
 
    var $skipClass = 'pear_class';
 
    
 
    function Class_Error($message = 'unknown error', $code = null,
 
                         $mode = null, $options = null, $userinfo = null)
 
    {
 
        $this->Error($message, $code, $mode, $options, $userinfo);
 
    }
 
}
 
 
$simple = new PEAR_Class;
 
 
// after call method, raises error in line 102
 
$simple->SimpleException();
 
 
// after call method, raises error in line 105
 
$simple->runSimpleException();
 
 
// sets design mode
 
$GLOBALS['_Design_Mode'] = true;
 
 
// after this call, raises error in line 83
 
$simple->runSimpleException();
 
 
// sets a user-defined error handler function.
 
setErrorHandler('handleError');
 
 
$simple->SimpleException();
 
 
?>
 
 |