PEAR::setErrorHandling()

PEAR::setErrorHandling() -- 通过PEAR包设置错误的处理操作

大纲

require_once 'PEAR.php';

void PEAR::setErrorHandling ([integer $mode = NULL [, mixed $options = NULL]])

内容

setErrorHandling() 能够被任何一个标准对象方法调用($obj->setErrorHandling) 并且作为一个静态方法(PEAR::setErrorHandling)调用. 如果调用为静态的, PEAR::setErrorHandling() 为PEAR的对象设置默认处理操作行为(全局错误处理行为). 如果作为一个对象方法调用 $obj->setErrorHandling()只能为那个(本地错误处理行为)对象设置默认的操作.

参数

这里是一个使用 setErrorHandling的例子:
<?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错误会引起PHP警告.
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.php中PEAR_Error的构造器会出现那些文件和行数.
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"
//输出"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"
		//列印"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
	 * 在PHP5中注意,如果方法没有被声明'静态'连接修正 , $this是没有被设置的. 
	 * PHP4中,$this被设置了,但没有设置给 myClass对象,于是也不能用.
	 * @param PEAR_Error 错误对象
	 * @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:
//这些原因 3个消息通过错误返回打印了:
// "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"
// 错误信息"oops",停止了错误脚本.
PEAR::raiseError('oops', MYCLASS_ERROR_CODE);
?>