setErrorHandling() 能够被任何一个标准对象方法调用($obj->setErrorHandling) 并且作为一个静态方法(PEAR::setErrorHandling)调用. 如果调用为静态的, PEAR::setErrorHandling() 为PEAR的对象设置默认处理操作行为(全局错误处理行为). 如果作为一个对象方法调用 $obj->setErrorHandling()只能为那个(本地错误处理行为)对象设置默认的操作.
integer $mode - 之后的常量,
PEAR_ERROR_RETURN 如果错误出现, 一个 PEAR_Error 从错误产生方法(error-generation method) (通常的 raiseError()返回.)
PEAR_ERROR_PRINT 像 PEAR_ERROR_RETURN, 但是一个错误信息会被加入列印.
PEAR_ERROR_TRIGGER 像 PEAR_ERROR_RETURN, 但是 PHP 建造函数(builtin-function) trigger_error() 会被PEAR_Rrror的带有错误信息的构造器调用.
PEAR_ERROR_DIE 这些代码会停止并且错误信息会列印到一个PEAR_Error实例.
PEAR_ERROR_CALLBACK如果一个错误产生,一个返回值传递给 $options 的调用.
PEAR_ERROR_EXCEPTION如果Zend Engine 2有安装,一个例外会使用PEAR_Error 对象抛出.
mixed $options - $options的值 依托于 $mode
PEAR_ERROR_PRINT 和
PEAR_ERROR_DIE 当打印错误消息的时候支持一个可选择的
printf() 格式字符串,这个字符串应该包含一个%s ,过去常常会被插入错误信息到字符串.
包含在错误信息前缀或者错误信息中有用的信息.但没有被包含在里面的,也可以被镶套进字符串.
PEAR_ERROR_TRIGGER 需求一个错误基本常数,给 trigger_error() 使用(可能是: E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). 如果错误常数不是这些有效错误常数中的一个,PHP警告会被触发.
PEAR_ERROR_CALLBACK 返回值必须是一个Pseudo-Type描述的函数命名的PHP手册(或者一个字符串一个数组)的片断. 返回值必须承担一个单一的参数,PEAR_Error 对象在一个错误条件下产生. 注意.如果返回值不是一个有效的返回,PHP错误会被触发.
这里是一个使用 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);
?> |