Sophie

Sophie

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

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
>Object Aggregation/Composition Functions</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="Referência das Funções"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="nsapi_virtual"
HREF="function.nsapi-virtual.html"><LINK
REL="NEXT"
TITLE="aggregate_info"
HREF="function.aggregate-info.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="reference"
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.nsapi-virtual.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.aggregate-info.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.objaggregation"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>CV. Object Aggregation/Composition Functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN150236"
></A
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Atenção</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>Este módulo é
 <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>EXPERIMENTAL</I
></SPAN
>. Isso quer dizer que o comportamento neste módulo ---
 incluindo suas funções e seus nomes, e TUDO mais que está documentado
 sobre esse módulo --- poderá mudar em futuras versões do PHP, SEM QUALQUER NOTIFICAÇÃO.
 Esteja avisado, e use este módulo por sua própria conta e risco.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="objaggregation.intro"
>Introdução</A
></H1
><P
>&#13;      In Object Oriented Programming, it is common to see the composition of
      simple classes (and/or instances) into a more complex one. This is a
      flexible strategy for building complicated objects and object
      hierarchies and can function as a dynamic alternative to multiple
      inheritance. There are two ways to perform class (and/or object)
      composition depending on the relationship between the composed
      elements: <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Association</I
></SPAN
> and
      <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Aggregation</I
></SPAN
>.
     </P
><P
>&#13;      An <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Association</I
></SPAN
> is a composition of independently constructed and
      externally visible parts.  When we associate classes or objects, each
      one keeps a reference to the ones it is associated with. When we
      associate classes statically, one class will contain a reference to an
      instance of the other class. For example:
      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN150247"
></A
><P
><B
>Exemplo 1. Class association</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">DateTime </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">DateTime</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// empty constructor<br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">now</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d H:i:s"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">Report </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$_dt</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// more properties ...<br /><br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">Report</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_dt </font><font color="#007700">= new </font><font color="#0000BB">DateTime</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// initialization code ...<br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">generateReport</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dateTime </font><font color="#007700">= </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_dt</font><font color="#007700">-&gt;</font><font color="#0000BB">now</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// more code ...<br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br /><br />&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// more methods ...<br /></font><font color="#007700">}<br /><br /></font><font color="#0000BB">$rep </font><font color="#007700">= new </font><font color="#0000BB">Report</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
      We can also associate instances at runtime by passing a reference in a
      constructor (or any other method), which allow us to dynamically change
      the association relationship between objects. We will modify the example 
      above to illustrate this point:
      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN150250"
></A
><P
><B
>Exemplo 2. Object association</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">DateTime </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// same as previous example<br /></font><font color="#007700">}<br /><br />class </font><font color="#0000BB">DateTimePlus </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$_format</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">DateTimePlus</font><font color="#007700">(</font><font color="#0000BB">$format</font><font color="#007700">=</font><font color="#DD0000">"Y-m-d H:i:s"</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_format </font><font color="#007700">= </font><font color="#0000BB">$format</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">now</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_format</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">Report </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$_dt</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// we'll keep the reference to DateTime here<br />&nbsp;&nbsp;&nbsp;// more properties ...<br /><br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">Report</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// do some initialization<br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">setDateTime</font><font color="#007700">(&amp;</font><font color="#0000BB">$dt</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_dt </font><font color="#007700">=&amp; </font><font color="#0000BB">$dt</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">generateReport</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dateTime </font><font color="#007700">= </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_dt</font><font color="#007700">-&gt;</font><font color="#0000BB">now</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// more code ...<br />&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br /><br />&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// more methods ...<br /></font><font color="#007700">}<br /><br /></font><font color="#0000BB">$rep </font><font color="#007700">= new </font><font color="#0000BB">Report</font><font color="#007700">();<br /></font><font color="#0000BB">$dt </font><font color="#007700">= new </font><font color="#0000BB">DateTime</font><font color="#007700">();<br /></font><font color="#0000BB">$dtp </font><font color="#007700">= new </font><font color="#0000BB">DateTimePlus</font><font color="#007700">(</font><font color="#DD0000">"l, F j, Y (h:i:s a, T)"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// generate report with simple date for web display<br /></font><font color="#0000BB">$rep</font><font color="#007700">-&gt;</font><font color="#0000BB">setDateTime</font><font color="#007700">(&amp;</font><font color="#0000BB">$dt</font><font color="#007700">);<br />echo </font><font color="#0000BB">$rep</font><font color="#007700">-&gt;</font><font color="#0000BB">generateReport</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// later on in the code ...<br /><br />// generate report with fancy date<br /></font><font color="#0000BB">$rep</font><font color="#007700">-&gt;</font><font color="#0000BB">setDateTime</font><font color="#007700">(&amp;</font><font color="#0000BB">$dtp</font><font color="#007700">);<br /></font><font color="#0000BB">$output </font><font color="#007700">= </font><font color="#0000BB">$rep</font><font color="#007700">-&gt;</font><font color="#0000BB">generateReport</font><font color="#007700">();<br /></font><font color="#FF8000">// save $output in database<br />// ... etc ... <br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;      <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Aggregation</I
></SPAN
>, on the other hand, implies 
      encapsulation (hidding) of the
      parts of the composition. We can aggregate classes by using a (static)
      inner class (PHP does not yet support inner classes), in this case the
      aggregated class definition is not accessible, except through the class
      that contains it. The aggregation of instances (object aggregation)
      involves the dynamic creation of subobjects inside an object, in the
      process, expanding the properties and methods of that object.
     </P
><P
>&#13;      Object aggregation is a natural way of representing a whole-part relationship, 
      (for example, molecules are aggregates of atoms), or can be used to
      obtain an effect equivalent to multiple inheritance, without having to
      permanently bind a subclass to two or more parent classes and their
      interfaces. In fact object aggregation can be more flexible, in which we
      can select what methods or properties to "inherit" in the aggregated
      object.
     </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="objaggregation.examples"
>Exemplos</A
></H1
><P
>&#13;      We define 3 classes, each implementing a different storage method:
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN150260"
></A
><P
><B
>Exemplo 3. storage_classes.inc</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">FileStorage </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$data</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">FileStorage</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">write</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#0000BB">name</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">WDDXStorage </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$version </font><font color="#007700">= </font><font color="#DD0000">"1.0"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$_id</font><font color="#007700">; </font><font color="#FF8000">// "private" variable<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">WDDXStorage</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_id </font><font color="#007700">= </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_genID</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">store</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_id</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$pid </font><font color="#007700">= </font><font color="#0000BB">wddx_packet_start</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">_id</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">wddx_add_vars</font><font color="#007700">(</font><font color="#0000BB">$pid</font><font color="#007700">, </font><font color="#DD0000">"this-&gt;data"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$packet </font><font color="#007700">= </font><font color="#0000BB">wddx_packet_end</font><font color="#007700">(</font><font color="#0000BB">$pid</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">$packet </font><font color="#007700">= </font><font color="#0000BB">wddx_serialize_value</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dbh </font><font color="#007700">= </font><font color="#0000BB">dba_open</font><font color="#007700">(</font><font color="#DD0000">"varstore"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">, </font><font color="#DD0000">"gdbm"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">dba_insert</font><font color="#007700">(</font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">uniqid</font><font color="#007700">(</font><font color="#DD0000">""</font><font color="#007700">, </font><font color="#0000BB">true</font><font color="#007700">)), </font><font color="#0000BB">$packet</font><font color="#007700">, </font><font color="#0000BB">$dbh</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">dba_close</font><font color="#007700">(</font><font color="#0000BB">$dbh</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// a private method<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">_genID</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">uniqid</font><font color="#007700">(</font><font color="#0000BB">rand</font><font color="#007700">(), </font><font color="#0000BB">true</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">DBStorage </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$dbtype </font><font color="#007700">= </font><font color="#DD0000">"mysql"</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">DBStorage</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">save</font><font color="#007700">() <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dbh </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"storage"</font><font color="#007700">, </font><font color="#0000BB">$dbh</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$serdata </font><font color="#007700">= </font><font color="#0000BB">serialize</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">data</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"insert into vars ('$serdata',now())"</font><font color="#007700">, </font><font color="#0000BB">$dbh</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_close</font><font color="#007700">(</font><font color="#0000BB">$dbh</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;     We then instantiate a couple of objects from the defined classes, and
     perform some aggregations and deaggregations, printing some object information
     along the way:
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN150265"
></A
><P
><B
>Exemplo 4. test_aggregation.php</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">include </font><font color="#DD0000">"storageclasses.inc"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// some utilty functions<br /><br /></font><font color="#007700">function </font><font color="#0000BB">p_arr</font><font color="#007700">(</font><font color="#0000BB">$arr</font><font color="#007700">) <br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$arr </font><font color="#007700">as </font><font color="#0000BB">$k </font><font color="#007700">=&gt; </font><font color="#0000BB">$v</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$out</font><font color="#007700">[] = </font><font color="#DD0000">"</font><font color="#007700">\t</font><font color="#DD0000">$k =&gt; $v"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">implode</font><font color="#007700">(</font><font color="#DD0000">"\n"</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">);<br />}<br /><br />function </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) <br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$out</font><font color="#007700">[] = </font><font color="#DD0000">"Class: " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">get_object_vars</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) as </font><font color="#0000BB">$var</font><font color="#007700">=&gt;</font><font color="#0000BB">$val</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$val</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$out</font><font color="#007700">[] = </font><font color="#DD0000">"property: $var (array)</font><font color="#007700">\n</font><font color="#DD0000">" </font><font color="#007700">. </font><font color="#0000BB">p_arr</font><font color="#007700">(</font><font color="#0000BB">$val</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">$out</font><font color="#007700">[] = </font><font color="#DD0000">"property: $var = $val"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">get_class_methods</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) as </font><font color="#0000BB">$method</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$out</font><font color="#007700">[] = </font><font color="#DD0000">"method: $method"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">implode</font><font color="#007700">(</font><font color="#DD0000">"\n"</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">);<br />}<br /><br /><br /></font><font color="#0000BB">$data </font><font color="#007700">= array(</font><font color="#0000BB">M_PI</font><font color="#007700">, </font><font color="#DD0000">"kludge != cruft"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// we create some basic objects<br /></font><font color="#0000BB">$fs </font><font color="#007700">= new </font><font color="#0000BB">FileStorage</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">);<br /></font><font color="#0000BB">$ws </font><font color="#007700">= new </font><font color="#0000BB">WDDXStorage</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// print information on the objects<br /></font><font color="#007700">echo </font><font color="#DD0000">"\$fs object\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"\n\$ws object\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$ws</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// do some aggregation<br /><br /></font><font color="#007700">echo </font><font color="#DD0000">"\nLet's aggregate \$fs to the WDDXStorage class\n"</font><font color="#007700">;<br /></font><font color="#0000BB">aggregate</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">, </font><font color="#DD0000">"WDDXStorage"</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\$fs object\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">"\nNow let us aggregate it to the DBStorage class\n"</font><font color="#007700">;<br /></font><font color="#0000BB">aggregate</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">, </font><font color="#DD0000">"DBStorage"</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\$fs object\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">"\nAnd finally deaggregate WDDXStorage\n"</font><font color="#007700">;<br /></font><font color="#0000BB">deaggregate</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">, </font><font color="#DD0000">"WDDXStorage"</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\$fs object\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">object_info</font><font color="#007700">(</font><font color="#0000BB">$fs</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;      We will now consider the output to understand some of the side-effects
      and limitation of object aggregation in PHP.
      First, the newly created <CODE
CLASS="varname"
>$fs</CODE
> and <CODE
CLASS="varname"
>$ws</CODE
>
      objects give the expected output (according to their respective class
      declaration). Note that for the purposes of object aggregation,
      <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>private elements of a class/object begin with an underscore 
      character ("_")</I
></SPAN
>, even though there is not real distinction between
      public and private class/object elements in PHP.
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN150273"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>$fs object
Class: filestorage
property: data (array)
    0 =&#62; 3.1415926535898
    1 =&#62; kludge != cruft
method: filestorage
method: write

$ws object
Class: wddxstorage
property: data (array)
    0 =&#62; 3.1415926535898
    1 =&#62; kludge != cruft
property: version = 1.0
property: _id = ID::9bb2b640764d4370eb04808af8b076a5
method: wddxstorage
method: store
method: _genid</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      We then aggregate <CODE
CLASS="varname"
>$fs</CODE
> with the
      <B
CLASS="classname"
>WDDXStorage</B
> class, and print out the object
      information. We can see now that even though nominally the
      <CODE
CLASS="varname"
>$fs</CODE
> object is still of
      <B
CLASS="classname"
>FileStorage</B
>, it now has the property
      <CODE
CLASS="varname"
>$version</CODE
>, and the method <B
CLASS="function"
>store()</B
>,
      both defined in <B
CLASS="classname"
>WDDXStorage</B
>. One important thing
      to note is that it has not aggregated the private elements defined in
      the class, which are present in the <CODE
CLASS="varname"
>$ws</CODE
> object. Also
      absent is the constructor from <B
CLASS="classname"
>WDDXStorage</B
>, which
      will not be logical to aggegate.
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN150286"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>Let's aggregate $fs to the WDDXStorage class
$fs object
Class: filestorage
property: data (array)
    0 =&#62; 3.1415926535898
    1 =&#62; kludge != cruft
property: version = 1.0
method: filestorage
method: write
method: store</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      The process of aggregation is cumulative, so when we aggregate
      <CODE
CLASS="varname"
>$fs</CODE
> with the class <B
CLASS="classname"
>DBStorage</B
>,
      generating an object that can use the storage methods of all the
      defined classes.
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN150292"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>Now let us aggregate it to the DBStorage class
$fs object
Class: filestorage
property: data (array)
    0 =&#62; 3.1415926535898
    1 =&#62; kludge != cruft
property: version = 1.0
property: dbtype = mysql
method: filestorage
method: write
method: store
method: save</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      Finally, the same way we aggregated properties and methods dynamically,
      we can also deaggregate them from the object. So, if we deaggregate the
      class <B
CLASS="classname"
>WDDXStorage</B
> from <CODE
CLASS="varname"
>$fs</CODE
>, we
      will obtain:
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN150298"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>And deaggregate the WDDXStorage methods and properties
$fs object
Class: filestorage
property: data (array)
    0 =&#62; 3.1415926535898
    1 =&#62; kludge != cruft
property: dbtype = mysql
method: filestorage
method: write
method: save</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      One point that we have not mentioned above, is that the process of
      aggregation will not override existing properties or methods in the
      objects. For example, the class <B
CLASS="classname"
>FileStorage</B
> defines a
      <CODE
CLASS="varname"
>$data</CODE
> property, and the class
      <B
CLASS="classname"
>WDDXStorage</B
> also defines a similar property 
      which will not override the one in the object acquired during
      instantiation from the class <B
CLASS="classname"
>FileStorage</B
>.
     </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Índice</B
></DT
><DT
><A
HREF="function.aggregate-info.html"
>aggregate_info</A
>&nbsp;--&nbsp;
     Returns an associative array of the methods and properties from
     each class that has been aggregated to the object
    </DT
><DT
><A
HREF="function.aggregate-methods-by-list.html"
>aggregate_methods_by_list</A
>&nbsp;--&nbsp;
     Selective dynamic class methods aggregation to an object
    </DT
><DT
><A
HREF="function.aggregate-methods-by-regexp.html"
>aggregate_methods_by_regexp</A
>&nbsp;--&nbsp;
     Selective class methods aggregation to an object using a regular
     expression
    </DT
><DT
><A
HREF="function.aggregate-methods.html"
>aggregate_methods</A
>&nbsp;--&nbsp;
     Dynamic class and object aggregation of methods
    </DT
><DT
><A
HREF="function.aggregate-properties-by-list.html"
>aggregate_properties_by_list</A
>&nbsp;--&nbsp;
     Selective dynamic class properties aggregation to an object
    </DT
><DT
><A
HREF="function.aggregate-properties-by-regexp.html"
>aggregate_properties_by_regexp</A
>&nbsp;--&nbsp;
     Selective class properties aggregation to an object using a regular
     expression
    </DT
><DT
><A
HREF="function.aggregate-properties.html"
>aggregate_properties</A
>&nbsp;--&nbsp;
     Dynamic aggregation of  class properties to an object
    </DT
><DT
><A
HREF="function.aggregate.html"
>aggregate</A
>&nbsp;--&nbsp;
     Dynamic class and object aggregation of methods and properties
    </DT
><DT
><A
HREF="function.aggregation-info.html"
>aggregation_info</A
>&nbsp;--&nbsp;Sinônimo de <A
HREF="function.aggregate-info.html"
><B
CLASS="function"
>aggregate_info()</B
></A
></DT
><DT
><A
HREF="function.deaggregate.html"
>deaggregate</A
>&nbsp;--&nbsp;
     Removes the aggregated methods and properties from an object
    </DT
></DL
></DIV
></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="function.nsapi-virtual.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.aggregate-info.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>nsapi_virtual</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>aggregate_info</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>