Sophie

Sophie

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

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
>create_function</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="Funções para manuseamento de funções"
HREF="ref.funchand.html"><LINK
REL="PREVIOUS"
TITLE="call_user_func"
HREF="function.call-user-func.html"><LINK
REL="NEXT"
TITLE="func_get_arg"
HREF="function.func-get-arg.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.call-user-func.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.func-get-arg.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.create-function"
></A
>create_function</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN59036"
></A
><P
>    (PHP 4 &#62;= 4.0.1, PHP 5)</P
>create_function&nbsp;--&nbsp;Cria uma função anônima (lambda-style)</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN59039"
></A
><H2
>Descrição</H2
>string <B
CLASS="methodname"
>create_function</B
> ( string args, string code )<BR
></BR
><P
>&#13;     Cria uma função anônima para os parâmetros passados, e
     retorna um nome único para isso. Usualmente o 
     <CODE
CLASS="parameter"
>args</CODE
> irá ser passado com um apóstrofo delimitando a string,
     e isso também é recomendado para
     <CODE
CLASS="parameter"
>code</CODE
>. O motivo para usar apóstrofo em torno da string,
     é para proteger que nome de variáveis sejam interpretados, caso contrário, 
     se você usar aspas duplas elas irão precisar de uma barra invertida em nomes de variáveis, e.g. 
     <TT
CLASS="literal"
>\$avar</TT
>.
    </P
><P
>&#13;     Você pode usar essas funções, para (por exemplo) criar uma função
     para que informações sejam colhidas todo o tempo:
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN59056"
></A
><P
><B
>Exemplo 1. 
       Criando uma função anônima com <B
CLASS="function"
>create_function()</B
>
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$newfunc </font><font color="#007700">= </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">,</font><font color="#DD0000">'return "ln($a) + ln($b) = ".log($a * $b);'</font><font color="#007700">);<br />echo </font><font color="#DD0000">"New anonymous function: $newfunc</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$newfunc</font><font color="#007700">(</font><font color="#0000BB">2</font><font color="#007700">,</font><font color="#0000BB">M_E</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// outputs<br />// New anonymous function: lambda_1<br />// ln(2) + ln(2.718281828459) = 1.6931471805599<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     Ou, talvez ter funções de manuseamento gerais que podem aplicar uma configuração
     de operações para a lista de parâmetros:
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN59062"
></A
><P
><B
>Exemplo 2. 
       Fazendo um processamento geral com a função
       <B
CLASS="function"
>create_function()</B
>
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#0000BB">$var1</font><font color="#007700">, </font><font color="#0000BB">$var2</font><font color="#007700">, </font><font color="#0000BB">$farr</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;for (</font><font color="#0000BB">$f</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$f </font><font color="#007700">&lt; </font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$farr</font><font color="#007700">); </font><font color="#0000BB">$f</font><font color="#007700">++)<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$farr</font><font color="#007700">[</font><font color="#0000BB">$f</font><font color="#007700">](</font><font color="#0000BB">$var1</font><font color="#007700">,</font><font color="#0000BB">$var2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// create a bunch of math functions<br /></font><font color="#0000BB">$f1 </font><font color="#007700">= </font><font color="#DD0000">'if ($a &gt;=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}'</font><font color="#007700">;<br /></font><font color="#0000BB">$f2 </font><font color="#007700">= </font><font color="#DD0000">"return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);"</font><font color="#007700">;<br /></font><font color="#0000BB">$f3 </font><font color="#007700">= </font><font color="#DD0000">'if ($a &gt; 0 &amp;&amp; $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }'</font><font color="#007700">;<br /></font><font color="#0000BB">$farr </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$x,$y'</font><font color="#007700">, </font><font color="#DD0000">'return "some trig: ".(sin($x) + $x*cos($y));'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$x,$y'</font><font color="#007700">, </font><font color="#DD0000">'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f1</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f2</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f3</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br />echo </font><font color="#DD0000">"\nUsing the first array of anonymous functions\n"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"parameters: 2.3445, M_PI\n"</font><font color="#007700">;<br /></font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#0000BB">2.3445</font><font color="#007700">, </font><font color="#0000BB">M_PI</font><font color="#007700">, </font><font color="#0000BB">$farr</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// now make a bunch of string processing functions<br /></font><font color="#0000BB">$garr </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$b,$a'</font><font color="#007700">,</font><font color="#DD0000">'if (strncmp($a,$b,3) == 0) return "** \"$a\" '</font><font color="#007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">,</font><font color="#DD0000">'; return "CRCs: ".crc32($a)." , ".crc32(b);'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">,</font><font color="#DD0000">'; return "similar(a,b) = ".similar_text($a,$b,&amp;$p)."($p%)";'</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />echo </font><font color="#DD0000">"\nUsing the second array of anonymous functions\n"</font><font color="#007700">;<br /></font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#DD0000">"Twas brilling and the slithy toves"</font><font color="#007700">, </font><font color="#DD0000">"Twas the night"</font><font color="#007700">, </font><font color="#0000BB">$garr</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       e quando você executa o código acima, a saída irá ser:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a/b) = 0.27122299212594

Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282 , 1908338681
similar(a,b) = 11(45.833333333333%)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     Porém talvez o uso mais comum das funções lambda-style (anonymous) 
     seja criar funções de retorno, por exemplo quando usando
     <A
HREF="function.array-walk.html"
><B
CLASS="function"
>array_walk()</B
></A
> ou <A
HREF="function.usort.html"
><B
CLASS="function"
>usort()</B
></A
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN59072"
></A
><P
><B
>Exemplo 3. Usando funções anônimas como funções de chamada de retorno</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$av </font><font color="#007700">= array(</font><font color="#DD0000">"the "</font><font color="#007700">,</font><font color="#DD0000">"a "</font><font color="#007700">,</font><font color="#DD0000">"that "</font><font color="#007700">,</font><font color="#DD0000">"this "</font><font color="#007700">);<br /></font><font color="#0000BB">array_walk</font><font color="#007700">(</font><font color="#0000BB">$av</font><font color="#007700">, </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'&amp;$v,$k'</font><font color="#007700">,</font><font color="#DD0000">'$v = $v."mango";'</font><font color="#007700">));<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$av</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// for PHP 3 use var_dump()<br />/*<br />outputs:<br />Array<br />(<br />&nbsp;&nbsp;[0] =&gt; the mango<br />&nbsp;&nbsp;[1] =&gt; a mango<br />&nbsp;&nbsp;[2] =&gt; that mango<br />&nbsp;&nbsp;[3] =&gt; this mango<br />)<br />*/<br /><br />// an array of strings ordered from shorter to longer<br /></font><font color="#0000BB">$sv </font><font color="#007700">= array(</font><font color="#DD0000">"small"</font><font color="#007700">,</font><font color="#DD0000">"larger"</font><font color="#007700">,</font><font color="#DD0000">"a big string"</font><font color="#007700">,</font><font color="#DD0000">"it is a string thing"</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">);<br /></font><font color="#FF8000">/*<br />outputs:<br />Array<br />(<br />&nbsp;&nbsp;[0] =&gt; small<br />&nbsp;&nbsp;[1] =&gt; larger<br />&nbsp;&nbsp;[2] =&gt; a big string<br />&nbsp;&nbsp;[3] =&gt; it is a string thing<br />)<br />*/<br /><br />// sort it from longer to shorter<br /></font><font color="#0000BB">usort</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">, </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">,</font><font color="#DD0000">'return strlen($b) - strlen($a);'</font><font color="#007700">));<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">);<br /></font><font color="#FF8000">/*<br />outputs:<br />Array<br />(<br />&nbsp;&nbsp;[0] =&gt; it is a string thing<br />&nbsp;&nbsp;[1] =&gt; a big string<br />&nbsp;&nbsp;[2] =&gt; larger<br />&nbsp;&nbsp;[3] =&gt; small<br />)<br />*/<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </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.call-user-func.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.func-get-arg.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>call_user_func</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.funchand.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>func_get_arg</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>