📄 function.set-error-handler.html
字号:
</dl> </p> <p class="para"> If the function returns <b><tt>FALSE</tt></b> then the normal error handler continues. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">error_types</tt></i></span> <dd> <p class="para"> Can be used to mask the triggering of the <i><tt class="parameter">error_handler</tt></i> function just like the <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> ini setting controls which errors are shown. Without this mask set the <i><tt class="parameter">error_handler</tt></i> will be called for every error regardless to the setting of the <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> setting. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a string containing the previously defined error handler (if any), or <b><tt>NULL</tt></b> on error. If the previous handler was a class method, this function will return an indexed array with the class and the method name. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.2.0</td> <td colspan="1" rowspan="1" align="left"> The error handler must return <b><tt>FALSE</tt></b> to populate <var class="varname"><a href="reserved.variables.phperrormsg.html" class="classname">$php_errormsg</a></var>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">error_types</tt></i> parameter was introduced. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> Instead of a function name, an array containing an object reference and a method name can also be supplied as the <i><tt class="parameter">error_handler</tt></i>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.2</td> <td colspan="1" rowspan="1" align="left"> Three optional parameters for the <i><tt class="parameter">error_handler</tt></i> user function was introduced. These are the filename, the line number, and the context. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Error handling with <b>set_error_handler()</b> and <a href="function.trigger-error.html" class="function">trigger_error()</a></b></p> <div class="example-contents"><p> The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// error handler function<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">myErrorHandler</span><span style="color: #007700">(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">$errfile</span><span style="color: #007700">, </span><span style="color: #0000BB">$errline</span><span style="color: #007700">)<br />{<br /> switch (</span><span style="color: #0000BB">$errno</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My ERROR</b> [$errno] $errstr<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" Fatal error on line $errline in file $errfile"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">", PHP " </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">" (" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_OS </span><span style="color: #007700">. </span><span style="color: #DD0000">")<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Aborting...<br />\n"</span><span style="color: #007700">;<br /> exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /> break;<br /><br /> case </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My WARNING</b> [$errno] $errstr<br />\n"</span><span style="color: #007700">;<br /> break;<br /><br /> case </span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My NOTICE</b> [$errno] $errstr<br />\n"</span><span style="color: #007700">;<br /> break;<br /><br /> default:<br /> echo </span><span style="color: #DD0000">"Unknown error type: [$errno] $errstr<br />\n"</span><span style="color: #007700">;<br /> break;<br /> }<br /><br /> </span><span style="color: #FF8000">/* Don't execute PHP internal error handler */<br /> </span><span style="color: #007700">return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// function to test the error handling<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">, </span><span style="color: #0000BB">$scale</span><span style="color: #007700">)<br />{<br /> if (!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">) || </span><span style="color: #0000BB">$scale </span><span style="color: #007700"><= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"log(x) for x <= 0 is undefined, you used: scale = $scale"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br /> }<br /><br /> if (!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Incorrect input vector, array of values expected"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #0000BB">$temp </span><span style="color: #007700">= array();<br /> foreach(</span><span style="color: #0000BB">$vect </span><span style="color: #007700">as </span><span style="color: #0000BB">$pos </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Value at position $pos is not a number, using 0 (zero)"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$temp</span><span style="color: #007700">[</span><span style="color: #0000BB">$pos</span><span style="color: #007700">] = </span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">) * </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /> }<br /><br /> return </span><span style="color: #0000BB">$temp</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// set to the user defined error handler<br /></span><span style="color: #0000BB">$old_error_handler </span><span style="color: #007700">= </span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">"myErrorHandler"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// trigger some errors, first define a mixed array with a non-numeric item<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"vector a\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #0000BB">5.5</span><span style="color: #007700">, </span><span style="color: #0000BB">43.3</span><span style="color: #007700">, </span><span style="color: #0000BB">21.11</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// now generate second array<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector b - a notice (b = log(PI) * a)\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Value at position $pos is not a number, using 0 (zero) */<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">M_PI</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// this is trouble, we pass a string instead of an array<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector c - a warning\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Incorrect input vector, array of values expected */<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #DD0000">"not array"</span><span style="color: #007700">, </span><span style="color: #0000BB">2.3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">); </span><span style="color: #FF8000">// NULL<br /><br />// this is a critical error, log of zero or negative number is undefined<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector d - fatal error\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* log(x) for x <= 0 is undefined, you used: scale = $scale" */<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, -</span><span style="color: #0000BB">2.5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">); </span><span style="color: #FF8000">// Never reached<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>vector aArray( [0] => 2 [1] => 3 [2] => foo [3] => 5.5 [4] => 43.3 [5] => 21.11)----vector b - a notice (b = log(PI) * a)<b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />Array( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281)----vector c - a warning<b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />NULL----vector d - fatal error<b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br /> Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />Aborting...<br /></pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.error-reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a></li> <li class="member"><a href="function.restore-error-handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a></li> <li class="member"><a href="function.trigger-error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a></li> <li class="member"><a href="errorfunc.constants.html" class="link">error level constants</a></li> <li class="member">information about the <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> type</li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.restore-exception-handler.html">restore_exception_handler</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.set-exception-handler.html">set_exception_handler</a></div> <div class="up"><a href="ref.errorfunc.html">Error Handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -