- trigger_error
- (PHP 4 >= 4.0.1, PHP 5)
- trigger_error -- 产生用户级别的 错误/警告/注意 信息
- 说明
- bool trigger_error ( string error_msg [, int error_type] )
- 在脚本中用户输入数据的位置,当用户的输入无效时触发错误的很有用的。
- 使用trigger_error()函数在处理错误上会更具优势,更易于处理错误。
- trigger_error()接受一个错误信息和一个常量作为参数。常量为:
- E_USER_ERROR - 致命的用户生成的 run-time 错误。错误无法恢复。脚本执行被中断。
- E_USER_WARNING - 非致命的用户生成的 run-time 警告。脚本执行不被中断。
- E_USER_NOTICE - 默认。用户生成的 run-time 通知。脚本发现了可能的错误,也有可能在脚本运行正常时发生。
- 返回值:如查错误类型不对,则返回FALSE,否则返回 TRUE
- 范例
- $test=2;
- if ($test>1)
- {
- trigger_error("Value must be 1 or below");
- }
- 可以设计一个错误处理器,然后再定义一个处理器选择函数set_error_handler()来使用这个错误处理器。
- 我们或许可以通过trigger_error()生成一个用户警告来代替,使程序更具有灵活性。
- Index2.php
- view plaincopy to clipboardprint?
- // PHP 4
- require_once('cmd_php4/Command.php');
- class CommandManager {
- var $cmdDir = "cmd_php4";
- function getCommandObject($cmd) {
- $path = "{$this->cmdDir}/{$cmd}.php";
- if (!file_exists($path)) {
- trigger_error("Cannot find $path", E_USER_ERROR);
- }
- require_once $path;
- if (!class_exists($cmd)) {
- trigger_error("class $cmd does not exist", E_USER_ERROR);
- }
- $ret = new $cmd();
- if (!is_a($ret, 'Command')) {
- trigger_error("$cmd is not a Command", E_USER_ERROR);
- }
- return $ret;
- }
- }
- // PHP 4
- require_once('cmd_php4/Command.php');
- class CommandManager {
- var $cmdDir = "cmd_php4";
- function getCommandObject($cmd) {
- $path = "{$this->cmdDir}/{$cmd}.php";
- if (!file_exists($path)) {
- trigger_error("Cannot find $path", E_USER_ERROR);
- }
- require_once $path;
- if (!class_exists($cmd)) {
- trigger_error("class $cmd does not exist", E_USER_ERROR);
- }
- $ret = new $cmd();
- if (!is_a($ret, 'Command')) {
- trigger_error("$cmd is not a Command", E_USER_ERROR);
- }
- return $ret;
- }
- }
- 如果你使用trigger_error()函数来替代die(),你的代码在处理错误上会更具优势,对于客户程序员来说更易于处理错误。trigger_error()接受一个错误信息和一个常量作为参数。常量为:
- 常量 含义
- E_USER_ERROR A fatal error
- E_USER_WARNING A non-fatal error
- E_USER_NOTICE A report that may not represent an error
- 你可以设计一个错误处理器,然后再定义一个处理器选择函数set_error_handler()来使用这个错误处理器。
- Index2.php 后半段
- view plaincopy to clipboardprint?
- // PHP 4
- function cmdErrorHandler($errnum, $errmsg, $file, $lineno) {
- if($errnum == E_USER_ERROR) {
- print "error: $errmsg\n";
- print "file: $file\n";
- print "line: $lineno\n";
- exit();
- }
- }
- $handler = set_error_handler('cmdErrorHandler');
- $mgr = new CommandManager();
- $cmd = $mgr->getCommandObject('realcommand');
- $cmd->execute();
- // PHP 4
- function cmdErrorHandler($errnum, $errmsg, $file, $lineno) {
- if($errnum == E_USER_ERROR) {
- print "error: $errmsg\n";
- print "file: $file\n";
- print "line: $lineno\n";
- exit();
- }
- }
- $handler = set_error_handler('cmdErrorHandler');
- $mgr = new CommandManager();
- $cmd = $mgr->getCommandObject('realcommand');
- $cmd->execute();
- set_error_handler()接受一个函数名作为参数。如果触发了一个错误,参数中的这个函数会被调用来处理错误。函数需要传入四个参数:错误标志,错误信息,出错文件,出错处的行数。你也可以将一组数组传递给set_error_handler()。数组中的第一个元素必须是错误处理器将调用的对象,第二个元素是错误处理函数的名称。可以看出,我们的错误处理器相当简单简陋,还可以改进。然而尽管你可以在错误处理器添加某些功能,如记录出错信息,输出debug数据等,这仍然是一个过于粗糙的错误处理途径。你的选择仅限于已经考虑到的出错情况。例如捕捉一个E_USER_ERROR错误
- 如果你愿意的话可以不中止脚本的执行(不使用exit()和die()),但如果这样做的话,可能会引起一些很微妙的bug,本来应该中止的程序却继续执行了。
posted @
2016-12-27 22:18
天涯海角路
阅读(
983)
评论()
编辑
收藏
举报