Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2433

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>set_error_handler</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Error Handling and Logging Functions"
HREF="ref.errorfunc.html"><LINK
REL="PREVIOUS"
TITLE="restore_error_handler"
HREF="function.restore-error-handler.html"><LINK
REL="NEXT"
TITLE="trigger_error"
HREF="function.trigger-error.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.restore-error-handler.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.trigger-error.html"
ACCESSKEY="N"
>Next</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="AEN23056"
></A
><P
>    (PHP 4 &#62;= 4.0.1)</P
>set_error_handler&nbsp;--&nbsp;
     Sets a user-defined error handler function.  
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN23059"
></A
><H2
>Description</H2
>string <B
CLASS="methodname"
>set_error_handler</B
> ( callback error_handler)<BR
></BR
><P
>&#13;     Sets a user function (<TT
CLASS="parameter"
><I
>error_handler</I
></TT
>) to handle
     errors in a script.  Returns the previously defined error handler (if
     any), or <TT
CLASS="constant"
><B
>FALSE</B
></TT
> on error.  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
>&#13;     The user function needs to accept 2 parameters: the error code, and a
     string describing the error. From PHP 4.0.2, an additional 3 optional
     parameters are 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).
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
      Instead of a function name, an array containing an object reference and
      a method name can also be supplied. (Since PHP 4.3.0)
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
      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
> and
      <TT
CLASS="constant"
><B
>E_COMPILE_WARNING</B
></TT
>.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     The example below shows the handling of 
     internal exceptions by triggering errors and handling them with a user
     defined function:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN23083"
></A
><P
><B
>Example 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
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

// redefine the user error constants - PHP 4 only
define ("FATAL",E_USER_ERROR);
define ("ERROR",E_USER_WARNING);
define ("WARNING",E_USER_NOTICE);

// set the error reporting level for this script
error_reporting (FATAL | ERROR | WARNING);

// error handler function
function myErrorHandler ($errno, $errstr, $errfile, $errline) {
  switch ($errno) {
  case FATAL:
    echo "&#60;b&#62;FATAL&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    echo "  Fatal error in line ".$errline." of file ".$errfile;
    echo ", PHP ".PHP_VERSION." (".PHP_OS.")&#60;br&#62;\n";
    echo "Aborting...&#60;br&#62;\n";
    exit(1);
    break;
  case ERROR:
    echo "&#60;b&#62;ERROR&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    break;
  case WARNING:
    echo "&#60;b&#62;WARNING&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    break;
    default:
    echo "Unkown error type: [$errno] $errstr&#60;br&#62;\n";
    break;
  }
}

// function to test the error handling
function scale_by_log ($vect, $scale) {
  if ( !is_numeric($scale) || $scale &#60;= 0 )
    trigger_error("log(x) for x &#60;= 0 is undefined, you used: scale = $scale",
      FATAL);
  if (!is_array($vect)) {
    trigger_error("Incorrect input vector, array of values expected", ERROR);
    return null;
  }
  for ($i=0; $i&#60;count($vect); $i++) {
    if (!is_numeric($vect[$i]))
      trigger_error("Value at position $i is not a number, using 0 (zero)", 
        WARNING);
    $temp[$i] = log($scale) * $vect[$i];
  }
  return $temp;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2,3,"foo",5.5,43.3,21.11);
print_r($a);

// now generate second array, generating a warning
echo "----\nvector b - a warning (b = log(PI) * a)\n";
$b = scale_by_log($a, M_PI);
print_r($b);

// this is trouble, we pass a string instead of an array
echo "----\nvector c - an error\n";
$c = scale_by_log("not array",2.3);
var_dump($c);

// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
$d = scale_by_log($a, -2.5);

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     And when you run this sample script, the output will be
     <DIV
CLASS="informalexample"
><A
NAME="AEN23088"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>vector a
Array
(
    [0] =&#62; 2
    [1] =&#62; 3
    [2] =&#62; foo
    [3] =&#62; 5.5
    [4] =&#62; 43.3
    [5] =&#62; 21.11
)
----
vector b - a warning (b = log(PI) * a)
&#60;b&#62;WARNING&#60;/b&#62; [1024] Value at position 2 is not a number, using 0 (zero)&#60;br&#62;
Array
(
    [0] =&#62; 2.2894597716988
    [1] =&#62; 3.4341896575482
    [2] =&#62; 0
    [3] =&#62; 6.2960143721717
    [4] =&#62; 49.566804057279
    [5] =&#62; 24.165247890281
)
----
vector c - an error
&#60;b&#62;ERROR&#60;/b&#62; [512] Incorrect input vector, array of values expected&#60;br&#62;
NULL
----
vector d - fatal error
&#60;b&#62;FATAL&#60;/b&#62; [256] log(x) for x &#60;= 0 is undefined, you used: scale = -2.5&#60;br&#62;
  Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)&#60;br&#62;
Aborting...&#60;br&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     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
>&#13;     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
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
     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
></BLOCKQUOTE
></DIV
><P
>&#13;     See also <A
HREF="function.error-reporting.html"
><B
CLASS="function"
>error_reporting()</B
></A
>,
     <A
HREF="function.restore-error-handler.html"
><B
CLASS="function"
>restore_error_handler()</B
></A
>,
     <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>, <A
HREF="function.user-error.html"
><B
CLASS="function"
>user_error()</B
></A
>
    </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-error-handler.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.trigger-error.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>restore_error_handler</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.errorfunc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>trigger_error</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>