PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/phpdoc/doc/set-error-handler.txt

https://github.com/c77cc/vim-devel
Plain Text | 202 lines | 163 code | 39 blank | 0 comment | 0 complexity | 055a489724fe2fb6ca35b3de93664aac MD5 | raw file
  1. set_error_handler -- Sets a user-defined error handler function
  2. mixed set_error_handler(callback error_handler [, int error_types])~
  3. Sets a user function ({error_handler}) to handle errors in a script.
  4. This function can be used for defining your own way of handling errors during
  5. runtime, for example in applications in which you need to do cleanup of
  6. data/files when a critical error happens, or when you need to trigger an error
  7. under certain conditions (using |trigger_error|).
  8. It is important to remember that the standard PHP error handler is completely
  9. bypassed. |error_reporting| settings will have no effect and your error
  10. handler will be called regardless - however you are still able to read the
  11. current value of error_reporting and act appropriately. Of particular note is
  12. that this value will be 0 if the statement that caused the error was prepended
  13. by the @ error-control operator.
  14. Also note that it is your responsibility to |die| if necessary. If the
  15. error-handler function returns, script execution will continue with the next
  16. statement after the one that caused an error.
  17. The following error types cannot be handled with a user defined function:
  18. E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
  19. E_COMPILE_WARNING, and most of E_STRICT raised in the file where
  20. |set_error_handler| is called.
  21. If errors occur before the script is executed (e.g. on file uploads) the
  22. custom error handler cannot be called since it is not registered at that time.
  23. {error_handler} The user function needs to accept two parameters: the error
  24. code, and a string describing the error. Then there are three optional
  25. parameters that may be supplied: the filename in which the error occurred, the
  26. line number in which the error occurred, and the context in which the error
  27. occurred (an array that points to the active symbol table at the point the
  28. error occurred). The function can be shown as:
  29. mixed (callback error_handler [, int error_types, int errno, string errstr [, string errfile [, int errline [, array errcontext]]]])~
  30. {errno} The first parameter, {errno}, contains the level of the error raised,
  31. as an integer.
  32. {errstr} The second parameter, {errstr}, contains the error message, as a
  33. string.
  34. {errfile} The third parameter is optional, {errfile}, which contains the
  35. filename that the error was raised in, as a string.
  36. {errline} The fourth parameter is optional, {errline}, which contains the line
  37. number the error was raised at, as an integer.
  38. {errcontext} The fifth parameter is optional, {errcontext}, which is an array
  39. that points to the active symbol table at the point the error occurred. In
  40. other words, {errcontext} will contain an array of every variable that existed
  41. in the scope the error was triggered in. User error handler must not modify
  42. error context.
  43. If the function returns FALSE then the normal error handler continues.
  44. {error_types} Can be used to mask the triggering of the {error_handler}
  45. function just like the error_reporting ini setting controls which errors are
  46. shown. Without this mask set the {error_handler} will be called for every
  47. error regardless to the setting of the error_reporting setting.
  48. Returns a string containing the previously defined error handler (if any). If
  49. the built-in error handler is used NULL is returned. NULL is also returned in
  50. case of an error such as an invalid callback. If the previous error handler
  51. was a class method, this function will return an indexed array with the class
  52. and the method name.
  53. Version Description 5.2.0 The error handler must return FALSE to populate
  54. $php_errormsg. 5.0.0 The {error_types} parameter was introduced. 4.3.0 Instead
  55. of a function name, an array containing an object reference and a method name
  56. can also be supplied as the {error_handler}. 4.0.2 Three optional parameters
  57. for the {error_handler} user function was introduced. These are the filename,
  58. the line number, and the context.
  59. Error handling with |set_error_handler| and |trigger_error| The example below
  60. shows the handling of internal exceptions by triggering errors and handling
  61. them with a user defined function:
  62. <?php >
  63. // error handler function
  64. function myErrorHandler($errno, $errstr, $errfile, $errline)
  65. {
  66. switch ($errno) {
  67. case E_USER_ERROR:
  68. echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
  69. echo " Fatal error on line $errline in file $errfile";
  70. echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
  71. echo "Aborting...<br />\n";
  72. exit(1);
  73. break;
  74. case E_USER_WARNING:
  75. echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
  76. break;
  77. case E_USER_NOTICE:
  78. echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
  79. break;
  80. default:
  81. echo "Unknown error type: [$errno] $errstr<br />\n";
  82. break;
  83. }
  84. /* Don't execute PHP internal error handler */
  85. return true;
  86. }
  87. // function to test the error handling
  88. function scale_by_log($vect, $scale)
  89. {
  90. if (!is_numeric($scale) || $scale <= 0) {
  91. trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
  92. }
  93. if (!is_array($vect)) {
  94. trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
  95. return null;
  96. }
  97. $temp = array();
  98. foreach($vect as $pos => $value) {
  99. if (!is_numeric($value)) {
  100. trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
  101. $value = 0;
  102. }
  103. $temp[$pos] = log($scale) * $value;
  104. }
  105. return $temp;
  106. }
  107. // set to the user defined error handler
  108. $old_error_handler = set_error_handler("myErrorHandler");
  109. // trigger some errors, first define a mixed array with a non-numeric item
  110. echo "vector a\n";
  111. $a = array(2, 3, "foo", 5.5, 43.3, 21.11);
  112. print_r($a);
  113. // now generate second array
  114. echo "----\nvector b - a notice (b = log(PI) * a)\n";
  115. /* Value at position $pos is not a number, using 0 (zero) */
  116. $b = scale_by_log($a, M_PI);
  117. print_r($b);
  118. // this is trouble, we pass a string instead of an array
  119. echo "----\nvector c - a warning\n";
  120. /* Incorrect input vector, array of values expected */
  121. $c = scale_by_log("not array", 2.3);
  122. var_dump($c); // NULL
  123. // this is a critical error, log of zero or negative number is undefined
  124. echo "----\nvector d - fatal error\n";
  125. /* log(x) for x <= 0 is undefined, you used: scale = $scale" */
  126. $d = scale_by_log($a, -2.5);
  127. var_dump($d); // Never reached
  128. ?>
  129. The above example will output something similar to:
  130. vector a
  131. Array
  132. (
  133. [0] => 2
  134. [1] => 3
  135. [2] => foo
  136. [3] => 5.5
  137. [4] => 43.3
  138. [5] => 21.11
  139. )
  140. ----
  141. vector b - a notice (b = log(PI) * a)
  142. <b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
  143. Array
  144. (
  145. [0] => 2.2894597716988
  146. [1] => 3.4341896575482
  147. [2] => 0
  148. [3] => 6.2960143721717
  149. [4] => 49.566804057279
  150. [5] => 24.165247890281
  151. )
  152. ----
  153. vector c - a warning
  154. <b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
  155. NULL
  156. ----
  157. vector d - fatal error
  158. <b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br />
  159. Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />
  160. Aborting...<br />
  161. ErrorException |error_reporting| |restore_error_handler| |trigger_error| error
  162. level constants information about the callback type
  163. vim:ft=help: