2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:57:16 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-21 23:56:11 +00:00
|
|
|
<appendix xml:id="errorfunc.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2002-10-08 10:44:00 +00:00
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
Below we can see an example of using the error handling capabilities in
|
2003-06-14 17:48:17 +00:00
|
|
|
PHP. We define an error handling function which logs the information into
|
2002-10-08 10:44:00 +00:00
|
|
|
a file (using an XML format), and e-mails the developer in case a critical
|
|
|
|
error in the logic happens.
|
|
|
|
<example>
|
|
|
|
<title>Using error handling in a script</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
// we will do our own error handling
|
|
|
|
error_reporting(0);
|
|
|
|
|
|
|
|
// user defined error handling function
|
2004-01-15 12:43:50 +00:00
|
|
|
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
|
|
|
|
{
|
2002-10-08 10:44:00 +00:00
|
|
|
// timestamp for the error entry
|
|
|
|
$dt = date("Y-m-d H:i:s (T)");
|
|
|
|
|
|
|
|
// define an assoc array of error string
|
|
|
|
// in reality the only entries we should
|
2004-03-02 15:22:06 +00:00
|
|
|
// consider are E_WARNING, E_NOTICE, E_USER_ERROR,
|
|
|
|
// E_USER_WARNING and E_USER_NOTICE
|
2002-10-08 10:44:00 +00:00
|
|
|
$errortype = array (
|
2006-06-08 11:19:12 +00:00
|
|
|
E_ERROR => 'Error',
|
|
|
|
E_WARNING => 'Warning',
|
|
|
|
E_PARSE => 'Parsing Error',
|
|
|
|
E_NOTICE => 'Notice',
|
|
|
|
E_CORE_ERROR => 'Core Error',
|
|
|
|
E_CORE_WARNING => 'Core Warning',
|
|
|
|
E_COMPILE_ERROR => 'Compile Error',
|
|
|
|
E_COMPILE_WARNING => 'Compile Warning',
|
|
|
|
E_USER_ERROR => 'User Error',
|
|
|
|
E_USER_WARNING => 'User Warning',
|
|
|
|
E_USER_NOTICE => 'User Notice',
|
|
|
|
E_STRICT => 'Runtime Notice',
|
2006-10-04 07:37:46 +00:00
|
|
|
E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
|
2002-10-08 10:44:00 +00:00
|
|
|
);
|
|
|
|
// set of errors for which a var trace will be saved
|
|
|
|
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
|
|
|
|
|
|
|
|
$err = "<errorentry>\n";
|
2003-12-15 16:55:22 +00:00
|
|
|
$err .= "\t<datetime>" . $dt . "</datetime>\n";
|
|
|
|
$err .= "\t<errornum>" . $errno . "</errornum>\n";
|
|
|
|
$err .= "\t<errortype>" . $errortype[$errno] . "</errortype>\n";
|
|
|
|
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
|
|
|
|
$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
|
|
|
|
$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";
|
2002-10-08 10:44:00 +00:00
|
|
|
|
2004-05-10 10:39:08 +00:00
|
|
|
if (in_array($errno, $user_errors)) {
|
2003-12-15 16:55:22 +00:00
|
|
|
$err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n";
|
2004-05-10 10:39:08 +00:00
|
|
|
}
|
2002-10-08 10:44:00 +00:00
|
|
|
$err .= "</errorentry>\n\n";
|
|
|
|
|
|
|
|
// for testing
|
|
|
|
// echo $err;
|
|
|
|
|
|
|
|
// save to the error log, and e-mail me if there is a critical user error
|
|
|
|
error_log($err, 3, "/usr/local/php4/error.log");
|
2004-03-02 15:22:06 +00:00
|
|
|
if ($errno == E_USER_ERROR) {
|
2003-12-15 16:55:22 +00:00
|
|
|
mail("phpdev@example.com", "Critical User Error", $err);
|
2004-03-02 15:22:06 +00:00
|
|
|
}
|
2002-10-08 10:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 12:43:50 +00:00
|
|
|
function distance($vect1, $vect2)
|
|
|
|
{
|
2002-10-08 10:44:00 +00:00
|
|
|
if (!is_array($vect1) || !is_array($vect2)) {
|
|
|
|
trigger_error("Incorrect parameters, arrays expected", E_USER_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($vect1) != count($vect2)) {
|
|
|
|
trigger_error("Vectors need to be of the same size", E_USER_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i=0; $i<count($vect1); $i++) {
|
|
|
|
$c1 = $vect1[$i]; $c2 = $vect2[$i];
|
|
|
|
$d = 0.0;
|
|
|
|
if (!is_numeric($c1)) {
|
|
|
|
trigger_error("Coordinate $i in vector 1 is not a number, using zero",
|
|
|
|
E_USER_WARNING);
|
|
|
|
$c1 = 0.0;
|
|
|
|
}
|
|
|
|
if (!is_numeric($c2)) {
|
|
|
|
trigger_error("Coordinate $i in vector 2 is not a number, using zero",
|
|
|
|
E_USER_WARNING);
|
|
|
|
$c2 = 0.0;
|
|
|
|
}
|
|
|
|
$d += $c2*$c2 - $c1*$c1;
|
|
|
|
}
|
|
|
|
return sqrt($d);
|
|
|
|
}
|
|
|
|
|
|
|
|
$old_error_handler = set_error_handler("userErrorHandler");
|
|
|
|
|
|
|
|
// undefined constant, generates a warning
|
|
|
|
$t = I_AM_NOT_DEFINED;
|
|
|
|
|
|
|
|
// define some "vectors"
|
2003-12-15 16:55:22 +00:00
|
|
|
$a = array(2, 3, "foo");
|
2002-10-08 10:44:00 +00:00
|
|
|
$b = array(5.5, 4.3, -1.6);
|
2004-03-02 15:22:06 +00:00
|
|
|
$c = array(1, -3);
|
2002-10-08 10:44:00 +00:00
|
|
|
|
|
|
|
// generate a user error
|
2003-12-18 17:46:56 +00:00
|
|
|
$t1 = distance($c, $b) . "\n";
|
2002-10-08 10:44:00 +00:00
|
|
|
|
|
|
|
// generate another user error
|
2003-12-18 17:46:56 +00:00
|
|
|
$t2 = distance($b, "i am not an array") . "\n";
|
2002-10-08 10:44:00 +00:00
|
|
|
|
|
|
|
// generate a warning
|
2003-12-18 17:46:56 +00:00
|
|
|
$t3 = distance($a, $b) . "\n";
|
2002-10-08 10:44:00 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2007-12-21 23:56:11 +00:00
|
|
|
</appendix>
|
2002-10-08 10:44:00 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-10-08 10:44:00 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|