trigger_error使用方法

  1. trigger_error
  2. (PHP 4 >= 4.0.1, PHP 5)
  3. trigger_error -- 产生用户级别的 错误/警告/注意 信息
  4. 说明
  5. bool trigger_error ( string error_msg [, int error_type] )
  6. 在脚本中用户输入数据的位置,当用户的输入无效时触发错误的很有用的。
  7. 使用trigger_error()函数在处理错误上会更具优势,更易于处理错误。
  8. trigger_error()接受一个错误信息和一个常量作为参数。常量为:
  9. E_USER_ERROR - 致命的用户生成的 run-time 错误。错误无法恢复。脚本执行被中断。
  10. E_USER_WARNING - 非致命的用户生成的 run-time 警告。脚本执行不被中断。
  11. E_USER_NOTICE - 默认。用户生成的 run-time 通知。脚本发现了可能的错误,也有可能在脚本运行正常时发生。
  12. 返回值:如查错误类型不对,则返回FALSE,否则返回 TRUE
  13. 范例
  14. $test=2;
  15. if ($test>1)
  16. {
  17. trigger_error("Value must be 1 or below");
  18. }
  19. 可以设计一个错误处理器,然后再定义一个处理器选择函数set_error_handler()来使用这个错误处理器。
  20. 我们或许可以通过trigger_error()生成一个用户警告来代替,使程序更具有灵活性。
  21. Index2.php
  22. view plaincopy to clipboardprint?
  23. // PHP 4
  24. require_once('cmd_php4/Command.php');
  25. class CommandManager {
  26. var $cmdDir = "cmd_php4";
  27. function getCommandObject($cmd) {
  28. $path = "{$this->cmdDir}/{$cmd}.php";
  29. if (!file_exists($path)) {
  30. trigger_error("Cannot find $path", E_USER_ERROR);
  31. }
  32. require_once $path;
  33. if (!class_exists($cmd)) {
  34. trigger_error("class $cmd does not exist", E_USER_ERROR);
  35. }
  36. $ret = new $cmd();
  37. if (!is_a($ret, 'Command')) {
  38. trigger_error("$cmd is not a Command", E_USER_ERROR);
  39. }
  40. return $ret;
  41. }
  42. }
  43. // PHP 4
  44. require_once('cmd_php4/Command.php');
  45. class CommandManager {
  46. var $cmdDir = "cmd_php4";
  47. function getCommandObject($cmd) {
  48. $path = "{$this->cmdDir}/{$cmd}.php";
  49. if (!file_exists($path)) {
  50. trigger_error("Cannot find $path", E_USER_ERROR);
  51. }
  52. require_once $path;
  53. if (!class_exists($cmd)) {
  54. trigger_error("class $cmd does not exist", E_USER_ERROR);
  55. }
  56. $ret = new $cmd();
  57. if (!is_a($ret, 'Command')) {
  58. trigger_error("$cmd is not a Command", E_USER_ERROR);
  59. }
  60. return $ret;
  61. }
  62. }
  63. 如果你使用trigger_error()函数来替代die(),你的代码在处理错误上会更具优势,对于客户程序员来说更易于处理错误。trigger_error()接受一个错误信息和一个常量作为参数。常量为:
  64. 常量 含义
  65. E_USER_ERROR A fatal error
  66. E_USER_WARNING A non-fatal error
  67. E_USER_NOTICE A report that may not represent an error
  68. 你可以设计一个错误处理器,然后再定义一个处理器选择函数set_error_handler()来使用这个错误处理器。
  69. Index2.php 后半段
  70. view plaincopy to clipboardprint?
  71. // PHP 4
  72. function cmdErrorHandler($errnum, $errmsg, $file, $lineno) {
  73. if($errnum == E_USER_ERROR) {
  74. print "error: $errmsg\n";
  75. print "file: $file\n";
  76. print "line: $lineno\n";
  77. exit();
  78. }
  79. }
  80. $handler = set_error_handler('cmdErrorHandler');
  81. $mgr = new CommandManager();
  82. $cmd = $mgr->getCommandObject('realcommand');
  83. $cmd->execute();
  84. // PHP 4
  85. function cmdErrorHandler($errnum, $errmsg, $file, $lineno) {
  86. if($errnum == E_USER_ERROR) {
  87. print "error: $errmsg\n";
  88. print "file: $file\n";
  89. print "line: $lineno\n";
  90. exit();
  91. }
  92. }
  93. $handler = set_error_handler('cmdErrorHandler');
  94. $mgr = new CommandManager();
  95. $cmd = $mgr->getCommandObject('realcommand');
  96. $cmd->execute();
  97. set_error_handler()接受一个函数名作为参数。如果触发了一个错误,参数中的这个函数会被调用来处理错误。函数需要传入四个参数:错误标志,错误信息,出错文件,出错处的行数。你也可以将一组数组传递给set_error_handler()。数组中的第一个元素必须是错误处理器将调用的对象,第二个元素是错误处理函数的名称。可以看出,我们的错误处理器相当简单简陋,还可以改进。然而尽管你可以在错误处理器添加某些功能,如记录出错信息,输出debug数据等,这仍然是一个过于粗糙的错误处理途径。你的选择仅限于已经考虑到的出错情况。例如捕捉一个E_USER_ERROR错误
  98. 如果你愿意的话可以不中止脚本的执行(不使用exit()和die()),但如果这样做的话,可能会引起一些很微妙的bug,本来应该中止的程序却继续执行了。
posted @ 2016-12-27 22:18  天涯海角路  阅读(983)  评论(0编辑  收藏  举报