Sophie

Sophie

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

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
>Patterns</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="Classes e Objetos (PHP 5)"
HREF="language.oop5.html"><LINK
REL="PREVIOUS"
TITLE="Iteração de Objetos"
HREF="language.oop5.iterations.html"><LINK
REL="NEXT"
TITLE="Métodos Mágicos"
HREF="language.oop5.magic.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="sect1"
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="language.oop5.iterations.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Capítulo 19. Classes e Objetos (PHP 5)</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop5.magic.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop5.patterns"
>Patterns</A
></H1
><P
>&#13;  Padrões (Patterns) são formas de descrever melhores práticas e bons projetos.
  Eles mostram soluções flexíveis para problemas comuns de programação.
 </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.patterns.factory"
>Factory</A
></H2
><P
>&#13;   O padrão Factory permite a instanciação de objetos em tempo de execução.
   É chamado de Factory uma vez que é responsável por "produzir" um objeto.
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5866"
></A
><P
><B
>Exemplo 19-24. Factory Method</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">class </font><font color="#0000BB">Exemplo<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Factory method<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function &amp;</font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#0000BB">$type</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (include_once </font><font color="#DD0000">'Drivers/' </font><font color="#007700">. </font><font color="#0000BB">$type </font><font color="#007700">. </font><font color="#DD0000">'.php'</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$classname </font><font color="#007700">= </font><font color="#DD0000">'Driver_' </font><font color="#007700">. </font><font color="#0000BB">$type</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new </font><font color="#0000BB">$classname</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">throw </font><font color="#007700">new </font><font color="#0000BB">Exception </font><font color="#007700">(</font><font color="#DD0000">'Driver não encontrado'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;    Definir esse método numa classe permite que drivers sejam carregados em
    tempo de execução. Se a classe <TT
CLASS="literal"
>Exemplo</TT
> fosse uma classe
    de abstração de banco de dados, carregar um driver
    <TT
CLASS="literal"
>MySQL</TT
> e um driver <TT
CLASS="literal"
>SQLite</TT
> poderia ser
    feito como se segue:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Carregar um driver MySQL<br /></font><font color="#0000BB">$mysql </font><font color="#007700">= </font><font color="#0000BB">Exemplo</font><font color="#007700">::</font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#DD0000">'MySQL'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Carregar um driver SQLite <br /></font><font color="#0000BB">$sqlite </font><font color="#007700">= </font><font color="#0000BB">Exemplo</font><font color="#007700">::</font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#DD0000">'SQLite'</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.patterns.singleton"
>Singleton</A
></H2
><P
>&#13;   O padrão Singleton se aplica em situações em que é preciso haver uma só
   instância de uma classe.
   O exemplo mais comum é uma conexão com um banco de dados.
   Implementar esse padrão permite ao programador fazer essa instância única
   ser facilmente acessível por muitos outros objetos.
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5877"
></A
><P
><B
>Exemplo 19-25. Função Singleton</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">class </font><font color="#0000BB">Exemplo<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Guarda uma instância da classe<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">static </font><font color="#0000BB">private $instance</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Um construtor privado<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Sou um construtor'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// O método singleton <br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">static </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">singleton</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!isset(</font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">__CLASS__</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance </font><font color="#007700">= new </font><font color="#0000BB">$c</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Método exemplo<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">latir</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Au!'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;    Isso permite que uma instância única de <TT
CLASS="literal"
>Exemplo</TT
> seja
    recuperada.
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Isso falharia porque o construtor é privado<br /></font><font color="#0000BB">$test </font><font color="#007700">= new </font><font color="#0000BB">Exemplo</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Isso sempre vai recuperar uma instância da classe<br /></font><font color="#0000BB">$test </font><font color="#007700">= </font><font color="#0000BB">Exemplo</font><font color="#007700">::</font><font color="#0000BB">singleton</font><font color="#007700">();<br /></font><font color="#0000BB">$test</font><font color="#007700">-&gt;</font><font color="#0000BB">latir</font><font color="#007700">();<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
></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="language.oop5.iterations.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="language.oop5.magic.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Iteração de Objetos</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop5.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Métodos Mágicos</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>