<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >set_error_handler</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Error Handling and Logging Functions" HREF="ref.errorfunc.html"><LINK REL="PREVIOUS" TITLE="restore_exception_handler" HREF="function.restore-exception-handler.html"><LINK REL="NEXT" TITLE="set_exception_handler" HREF="function.set-exception-handler.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.restore-exception-handler.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.set-exception-handler.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.set-error-handler" ></A >set_error_handler</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN47782" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >set_error_handler -- Sets a user-defined error handler function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN47785" ></A ><H2 >说明</H2 >mixed <B CLASS="methodname" >set_error_handler</B > ( callback error_handler [, int error_types] )<BR ></BR ><P > Sets a user function (<CODE CLASS="parameter" >error_handler</CODE >) to handle errors in a script. </P ><P > This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A >). </P ><P > It is important to remember that the standard PHP error handler is completely bypassed. <A HREF="function.error-reporting.html" ><B CLASS="function" >error_reporting()</B ></A > settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the <A HREF="language.operators.errorcontrol.html" >@ error-control operator</A >. </P ><P > Also note that it is your responsibility to <A HREF="function.die.html" ><B CLASS="function" >die()</B ></A > if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error. </P ><P > The following error types cannot be handled with a user defined function: <TT CLASS="constant" ><B >E_ERROR</B ></TT >, <TT CLASS="constant" ><B >E_PARSE</B ></TT >, <TT CLASS="constant" ><B >E_CORE_ERROR</B ></TT >, <TT CLASS="constant" ><B >E_CORE_WARNING</B ></TT >, <TT CLASS="constant" ><B >E_COMPILE_ERROR</B ></TT >, <TT CLASS="constant" ><B >E_COMPILE_WARNING</B ></TT >, and most of <TT CLASS="constant" ><B >E_STRICT</B ></TT > raised in the file where <B CLASS="function" >set_error_handler()</B > is called. </P ><P > If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN47816" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >error_handler</CODE ></DT ><DD ><P > The user function needs to accept two parameters: the error code, and a string describing the error. Then there are three optional parameters that may be supplied: the filename in which the error occurred, the line number in which the error occurred, and the context in which the error occurred (an array that points to the active symbol table at the point the error occurred). The function can be shown as: </P ><P > <B CLASS="methodname" ><TT CLASS="replaceable" ><I >handler</I ></TT ></B > ( int errno, string errstr [, string errfile [, int errline [, array errcontext]]] )<BR ></BR > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >errno</CODE ></DT ><DD ><P > The first parameter, <CODE CLASS="parameter" >errno</CODE >, contains the level of the error raised, as an integer. </P ></DD ><DT ><CODE CLASS="parameter" >errstr</CODE ></DT ><DD ><P > The second parameter, <CODE CLASS="parameter" >errstr</CODE >, contains the error message, as a string. </P ></DD ><DT ><CODE CLASS="parameter" >errfile</CODE ></DT ><DD ><P > The third parameter is optional, <CODE CLASS="parameter" >errfile</CODE >, which contains the filename that the error was raised in, as a string. </P ></DD ><DT ><CODE CLASS="parameter" >errline</CODE ></DT ><DD ><P > The fourth parameter is optional, <CODE CLASS="parameter" >errline</CODE >, which contains the line number the error was raised at, as an integer. </P ></DD ><DT ><CODE CLASS="parameter" >errcontext</CODE ></DT ><DD ><P > The fifth parameter is optional, <CODE CLASS="parameter" >errcontext</CODE >, which is an array that points to the active symbol table at the point the error occurred. In other words, <CODE CLASS="parameter" >errcontext</CODE > will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context. </P ></DD ></DL ></DIV > </P ></DD ><DT ><CODE CLASS="parameter" >error_types</CODE ></DT ><DD ><P > Can be used to mask the triggering of the <CODE CLASS="parameter" >error_handler</CODE > function just like the <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > ini setting controls which errors are shown. Without this mask set the <CODE CLASS="parameter" >error_handler</CODE > will be called for every error regardless to the setting of the <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > setting. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN47885" ></A ><H2 >返回值</H2 ><P > Returns a string containing the previously defined error handler (if any), or <TT CLASS="constant" ><B >NULL</B ></TT > 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" ><A NAME="AEN47889" ></A ><H2 >更新日志</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN47892" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >版本</TH ><TH >说明</TH ></TR ></THEAD ><TBODY ><TR ><TD >5.2.0</TD ><TD > The error handler must return <TT CLASS="constant" ><B >FALSE</B ></TT > to populate <A HREF="reserved.variables.html#reserved.variables.phperrormsg" >$php_errormsg</A >. </TD ></TR ><TR ><TD >5.0.0</TD ><TD > The <CODE CLASS="parameter" >error_types</CODE > parameter was introduced. </TD ></TR ><TR ><TD >4.3.0</TD ><TD > Instead of a function name, an array containing an object reference and a method name can also be supplied as the <CODE CLASS="parameter" >error_handler</CODE >. </TD ></TR ><TR ><TD >4.0.2</TD ><TD > Three optional parameters for the <CODE CLASS="parameter" >error_handler</CODE > user function was introduced. These are the filename, the line number, and the context. </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN47916" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN47919" ></A ><P ><B >例 1. Error handling with <B CLASS="function" >set_error_handler()</B > and <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A ></B ></P ><P > The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// error handler function<br /></font><font color="#007700">function </font><font color="#0000BB">myErrorHandler</font><font color="#007700">(</font><font color="#0000BB">$errno</font><font color="#007700">, </font><font color="#0000BB">$errstr</font><font color="#007700">, </font><font color="#0000BB">$errfile</font><font color="#007700">, </font><font color="#0000BB">$errline</font><font color="#007700">)<br />{<br /> switch (</font><font color="#0000BB">$errno</font><font color="#007700">) {<br /> case </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">:<br /> echo </font><font color="#DD0000">"<b>My ERROR</b> </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" Fatal error on line $errline in file $errfile"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">", PHP " </font><font color="#007700">. </font><font color="#0000BB">PHP_VERSION </font><font color="#007700">. </font><font color="#DD0000">" (" </font><font color="#007700">. </font><font color="#0000BB">PHP_OS </font><font color="#007700">. </font><font color="#DD0000">")<br />\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"Aborting...<br />\n"</font><font color="#007700">;<br /> exit(</font><font color="#0000BB">1</font><font color="#007700">);<br /> break;<br /><br /> case </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">:<br /> echo </font><font color="#DD0000">"<b>My WARNING</b> </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> break;<br /><br /> case </font><font color="#0000BB">E_USER_NOTICE</font><font color="#007700">:<br /> echo </font><font color="#DD0000">"<b>My NOTICE</b> </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> break;<br /><br /> default:<br /> echo </font><font color="#DD0000">"Unknown error type: </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> break;<br /> }<br /><br /> </font><font color="#FF8000">/* Don't execute PHP internal error handler */<br /> </font><font color="#007700">return </font><font color="#0000BB">true</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// function to test the error handling<br /></font><font color="#007700">function </font><font color="#0000BB">scale_by_log</font><font color="#007700">(</font><font color="#0000BB">$vect</font><font color="#007700">, </font><font color="#0000BB">$scale</font><font color="#007700">)<br />{<br /> if (!</font><font color="#0000BB">is_numeric</font><font color="#007700">(</font><font color="#0000BB">$scale</font><font color="#007700">) || </font><font color="#0000BB">$scale </font><font color="#007700"><= </font><font color="#0000BB">0</font><font color="#007700">) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"log(x) for x <= 0 is undefined, you used: scale = $scale"</font><font color="#007700">, </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">);<br /> }<br /><br /> if (!</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$vect</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Incorrect input vector, array of values expected"</font><font color="#007700">, </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">);<br /> return </font><font color="#0000BB">null</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#0000BB">$temp </font><font color="#007700">= array();<br /> foreach(</font><font color="#0000BB">$vect </font><font color="#007700">as </font><font color="#0000BB">$pos </font><font color="#007700">=> </font><font color="#0000BB">$value</font><font color="#007700">) {<br /> if (!</font><font color="#0000BB">is_numeric</font><font color="#007700">(</font><font color="#0000BB">$value</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Value at position $pos is not a number, using 0 (zero)"</font><font color="#007700">, </font><font color="#0000BB">E_USER_NOTICE</font><font color="#007700">);<br /> </font><font color="#0000BB">$value </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$temp</font><font color="#007700">[</font><font color="#0000BB">$pos</font><font color="#007700">] = </font><font color="#0000BB">log</font><font color="#007700">(</font><font color="#0000BB">$scale</font><font color="#007700">) * </font><font color="#0000BB">$value</font><font color="#007700">;<br /> }<br /><br /> return </font><font color="#0000BB">$temp</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// set to the user defined error handler<br /></font><font color="#0000BB">$old_error_handler </font><font color="#007700">= </font><font color="#0000BB">set_error_handler</font><font color="#007700">(</font><font color="#DD0000">"myErrorHandler"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// trigger some errors, first define a mixed array with a non-numeric item<br /></font><font color="#007700">echo </font><font color="#DD0000">"vector a\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$a </font><font color="#007700">= array(</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"foo"</font><font color="#007700">, </font><font color="#0000BB">5.5</font><font color="#007700">, </font><font color="#0000BB">43.3</font><font color="#007700">, </font><font color="#0000BB">21.11</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// now generate second array<br /></font><font color="#007700">echo </font><font color="#DD0000">"----\nvector b - a notice (b = log(PI) * a)\n"</font><font color="#007700">;<br /></font><font color="#FF8000">/* Value at position $pos is not a number, using 0 (zero) */<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">scale_by_log</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">M_PI</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$b</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// this is trouble, we pass a string instead of an array<br /></font><font color="#007700">echo </font><font color="#DD0000">"----\nvector c - a warning\n"</font><font color="#007700">;<br /></font><font color="#FF8000">/* Incorrect input vector, array of values expected */<br /></font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">scale_by_log</font><font color="#007700">(</font><font color="#DD0000">"not array"</font><font color="#007700">, </font><font color="#0000BB">2.3</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$c</font><font color="#007700">); </font><font color="#FF8000">// NULL<br /><br />// this is a critical error, log of zero or negative number is undefined<br /></font><font color="#007700">echo </font><font color="#DD0000">"----\nvector d - fatal error\n"</font><font color="#007700">;<br /></font><font color="#FF8000">/* log(x) for x <= 0 is undefined, you used: scale = $scale" */<br /></font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">scale_by_log</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, -</font><font color="#0000BB">2.5</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$d</font><font color="#007700">); </font><font color="#FF8000">// Never reached<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例的输出类似于:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >vector a Array ( [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 ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN47927" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.error-reporting.html" ><B CLASS="function" >error_reporting()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.restore-error-handler.html" ><B CLASS="function" >restore_error_handler()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A ></TD ></TR ><TR ><TD ><A HREF="ref.errorfunc.html#errorfunc.constants" >error level constants</A ></TD ></TR ><TR ><TD >有关 <A HREF="language.pseudo-types.html#language.types.callback" >callback</A > 类型的信息</TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.restore-exception-handler.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.set-exception-handler.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >restore_exception_handler</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.errorfunc.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >set_exception_handler</TD ></TR ></TABLE ></DIV ></BODY ></HTML >