Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 9575

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!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="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Erros e Logs"
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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.restore-exception-handler.html"
ACCESSKEY="P"
>Anterior</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"
>Próxima</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="AEN44264"
></A
><P
>    (PHP 4 &#62;= 4.0.1, PHP 5)</P
>set_error_handler&nbsp;--&nbsp;
     Define uma função do usuário para manusear os erros.
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN44267"
></A
><H2
>Descrição</H2
>string <B
CLASS="methodname"
>set_error_handler</B
> ( callback error_handler )<BR
></BR
><P
>&#13;     Define uma função do usuário (<CODE
CLASS="parameter"
>error_handler</CODE
>) para manusear os 
     erros no script. Retorna a função definida anteriormente (se houver alguma),
     ou <TT
CLASS="constant"
><B
>FALSE</B
></TT
> em caso de erro. Esta função pode ser usada para definir o seu próprio
     meio de manusear os erros durante a execução do script,
     por exemplo em aplicações que você precisa fazer uma limpesa de dados/arquivos quando 
     um erro crítico acontece, ou quando você quer criar um erro
     sobre certas condições (usando
     <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>).
    </P
><P
>&#13;     A função do usuario precisa aceitar dois parâmetros: o código do erro e a string
     descrevendo o erro. Apartir do PHP 4.0.2, três parâmetros opcionais
     são fornecidos: o nome do erro onde o arquivo aconteceu, o número
     da linha onde o erro aconteceu, e o contexto onde o erro aconteceu
     (uma matriz que aponta para a tabela de símbolos ativos no
     ponto onde aconteceu o erro).
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      Ao invés de um nome de função, uma matriz contendo uma referência a um objeto
      e um nome de metodo pode ser indicado (Apartir do PHP 4.3.0).
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      Os seguintes tipos de erro não podem ser manuseados por uma função definida pelo usuário: 
      <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
> e
      <TT
CLASS="constant"
><B
>E_COMPILE_WARNING</B
></TT
>.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     O exemplo abaixo mostra como manusear exceções criando
     erros e gerenciando erros com uma função definida pelo usuário:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN44291"
></A
><P
><B
>Exemplo 1. 
       Manuseando erros com <B
CLASS="function"
>set_error_handler()</B
> e
       <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#FF8000">// redefine the user error constants - PHP 4 only<br /></font><font color="#0000BB">define</font><font color="#007700">(</font><font color="#DD0000">"FATAL"</font><font color="#007700">, </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">);<br /></font><font color="#0000BB">define</font><font color="#007700">(</font><font color="#DD0000">"ERROR"</font><font color="#007700">, </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">);<br /></font><font color="#0000BB">define</font><font color="#007700">(</font><font color="#DD0000">"WARNING"</font><font color="#007700">, </font><font color="#0000BB">E_USER_NOTICE</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// set the error reporting level for this script<br /></font><font color="#0000BB">error_reporting</font><font color="#007700">(</font><font color="#0000BB">FATAL </font><font color="#007700">| </font><font color="#0000BB">ERROR </font><font color="#007700">| </font><font color="#0000BB">WARNING</font><font color="#007700">);<br /><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 />&nbsp;&nbsp;switch (</font><font color="#0000BB">$errno</font><font color="#007700">) {<br />&nbsp;&nbsp;case </font><font color="#0000BB">FATAL</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;b&gt;FATAL&lt;/b&gt; </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr&lt;br /&gt;</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&nbsp;&nbsp;Fatal error in line $errline of file $errfile"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;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">")&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Aborting...&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</font><font color="#0000BB">1</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;case </font><font color="#0000BB">ERROR</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;b&gt;ERROR&lt;/b&gt; </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr&lt;br /&gt;</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;case </font><font color="#0000BB">WARNING</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;b&gt;WARNING&lt;/b&gt; </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr&lt;br /&gt;</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Unkown error type: </font><font color="#007700">[</font><font color="#DD0000">$errno</font><font color="#007700">]</font><font color="#DD0000"> $errstr&lt;br /&gt;</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;}<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 />&nbsp;&nbsp;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">&lt;= </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"log(x) for x &lt;= 0 is undefined, you used: scale = $scale"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">FATAL</font><font color="#007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;if (!</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$vect</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</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">ERROR</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">null</font><font color="#007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;for (</font><font color="#0000BB">$i</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i</font><font color="#007700">&lt;</font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$vect</font><font color="#007700">); </font><font color="#0000BB">$i</font><font color="#007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">is_numeric</font><font color="#007700">(</font><font color="#0000BB">$vect</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">]))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Value at position $i is not a number, using 0 (zero)"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">WARNING</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$temp</font><font color="#007700">[</font><font color="#0000BB">$i</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">$vect</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">];<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;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, generating a warning<br /></font><font color="#007700">echo </font><font color="#DD0000">"----\nvector b - a warning (b = log(PI) * a)\n"</font><font color="#007700">;<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 - an error\n"</font><font color="#007700">;<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">);<br /><br /></font><font color="#FF8000">// 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="#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 /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       E quando você usar este script de exemplo, a saída será:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>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
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     É importante lembrar que o gerenciamento de erro do padrão do PHP é completamente
     ignorado. As definições de <A
HREF="function.error-reporting.html"
><B
CLASS="function"
>error_reporting()</B
></A
> não terão efeito
     e seu gerenciador de erro será chamado. Em todo o caso, você ainda poderá
     ler o valor atual de <A
HREF="ref.errorfunc.html#ini.error-reporting"
>error_reporting</A
> e
     ajir apropriadamente. Uma nota em particular é que este valor será 0 se
     o comando que causou o erro for precedido pelo
     <A
HREF="language.operators.errorcontrol.html"
>operador de 
     controle de erro @</A
>.
    </P
><P
>&#13;     Note também que é sua responsabilidade usar o <A
HREF="function.die.html"
><B
CLASS="function"
>die()</B
></A
>
     se necessário. Se a função que gerencia os erros retornar, a execução do script
     irá continuar a apartir do comando seguinte ao que causou o erro
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     Se o erro acontecer antes da execução do script(ex. enquanto o arquivo é carregado)
     a função defina pelo usuario não será chamada já que não estará ainda registrada.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     Veja também <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
> e 
     <A
HREF="ref.errorfunc.html#errorfunc.constants"
>constantes de níveis de erro</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-exception-handler.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.set-exception-handler.html"
ACCESSKEY="N"
>Próxima</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"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>set_exception_handler</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>