Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > fedef0ce54a3761fc1862157672b700c > files > 3759

php-manual-ar-5.1.6-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Comparing objects</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Handleiding"
HREF="index.html"><LINK
REL="UP"
TITLE="Classes and Objects (PHP 4)"
HREF="language.oop.html"><LINK
REL="PREVIOUS"
TITLE="References inside the constructor"
HREF="language.oop.newref.html"><LINK
REL="NEXT"
TITLE="Classes and Objects (PHP 5)"
HREF="language.oop5.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></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"
>PHP Handleiding</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.oop.newref.html"
ACCESSKEY="P"
>Terug</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Hoofdstuk 13. Classes and Objects (PHP 4)</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop5.html"
ACCESSKEY="N"
>Volgende</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop.object-comparison"
>Comparing objects</A
></H1
><P
>&#13;   In PHP 4, objects are compared in a very simple manner, namely: Two object
   instances are equal if they have the same attributes and values, and are
   instances of the same class. Similar rules are applied when comparing two
   objects using the identity operator (<VAR
CLASS="literal"
>===</VAR
>).
  </P
><P
>&#13;   If we were to execute the code in the example below:
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN4874"
></A
><P
><B
>Voorbeeld 13-1. Example of object comparison in PHP 4</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">bool2str</font><font color="#007700">(</font><font color="#0000BB">$bool</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$bool </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#DD0000">'FALSE'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#DD0000">'TRUE'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">compareObjects</font><font color="#007700">(&amp;</font><font color="#0000BB">$o1</font><font color="#007700">, &amp;</font><font color="#0000BB">$o2</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 == o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 != o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">!= </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 === o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">=== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 !== o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">!== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />class </font><font color="#0000BB">Flag </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$flag</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Flag</font><font color="#007700">(</font><font color="#0000BB">$flag</font><font color="#007700">=</font><font color="#0000BB">true</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">flag </font><font color="#007700">= </font><font color="#0000BB">$flag</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">SwitchableFlag </font><font color="#007700">extends </font><font color="#0000BB">Flag </font><font color="#007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">turnOn</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">flag </font><font color="#007700">= </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">turnOff</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">flag </font><font color="#007700">= </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">Flag</font><font color="#007700">();<br /></font><font color="#0000BB">$p </font><font color="#007700">= new </font><font color="#0000BB">Flag</font><font color="#007700">(</font><font color="#0000BB">false</font><font color="#007700">);<br /></font><font color="#0000BB">$q </font><font color="#007700">= new </font><font color="#0000BB">Flag</font><font color="#007700">();<br /><br /></font><font color="#0000BB">$r </font><font color="#007700">= new </font><font color="#0000BB">SwitchableFlag</font><font color="#007700">();<br /><br />echo </font><font color="#DD0000">"Compare instances created with the same parameters\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjects</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$q</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nCompare instances created with different parameters\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjects</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$p</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nCompare an instance of a parent class with one from a subclass\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjects</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$r</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   We will see:
   <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Compare instances created with the same parameters
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Compare instances created with different parameters
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Compare an instance of a parent class with one from a subclass
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE</PRE
></TD
></TR
></TABLE
>
   Which is the output we will expect to obtain given the comparison rules
   above. Only instances with the same values for their attributes and from the same
   class are considered equal and identical.
  </P
><P
>&#13;   Even in the cases where we have object composition, the same comparison
   rules apply. In the example below we create a container class that stores
   an associative array of <B
CLASS="classname"
>Flag</B
> objects.
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN4880"
></A
><P
><B
>Voorbeeld 13-2. Compound object comparisons in PHP 4</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">FlagSet </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$set</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">FlagSet</font><font color="#007700">(</font><font color="#0000BB">$flagArr </font><font color="#007700">= array()) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">set </font><font color="#007700">= </font><font color="#0000BB">$flagArr</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">addFlag</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$flag</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">set</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">] = </font><font color="#0000BB">$flag</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">removeFlag</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">array_key_exists</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">set</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">set</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /><br /></font><font color="#0000BB">$u </font><font color="#007700">= new </font><font color="#0000BB">FlagSet</font><font color="#007700">();<br /></font><font color="#0000BB">$u</font><font color="#007700">-&gt;</font><font color="#0000BB">addFlag</font><font color="#007700">(</font><font color="#DD0000">'flag1'</font><font color="#007700">, </font><font color="#0000BB">$o</font><font color="#007700">);<br /></font><font color="#0000BB">$u</font><font color="#007700">-&gt;</font><font color="#0000BB">addFlag</font><font color="#007700">(</font><font color="#DD0000">'flag2'</font><font color="#007700">, </font><font color="#0000BB">$p</font><font color="#007700">);<br /></font><font color="#0000BB">$v </font><font color="#007700">= new </font><font color="#0000BB">FlagSet</font><font color="#007700">(array(</font><font color="#DD0000">'flag1'</font><font color="#007700">=&gt;</font><font color="#0000BB">$q</font><font color="#007700">, </font><font color="#DD0000">'flag2'</font><font color="#007700">=&gt;</font><font color="#0000BB">$p</font><font color="#007700">));<br /></font><font color="#0000BB">$w </font><font color="#007700">= new </font><font color="#0000BB">FlagSet</font><font color="#007700">(array(</font><font color="#DD0000">'flag1'</font><font color="#007700">=&gt;</font><font color="#0000BB">$q</font><font color="#007700">));<br /><br />echo </font><font color="#DD0000">"\nComposite objects u(o,p) and v(q,p)\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjects</font><font color="#007700">(</font><font color="#0000BB">$u</font><font color="#007700">, </font><font color="#0000BB">$v</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nu(o,p) and w(q)\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjects</font><font color="#007700">(</font><font color="#0000BB">$u</font><font color="#007700">, </font><font color="#0000BB">$w</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   Which gives the expected output:
   <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Composite objects u(o,p) and v(q,p)
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

u(o,p) and w(q)
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE</PRE
></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="language.oop.newref.html"
ACCESSKEY="P"
>Terug</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Begin</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.oop5.html"
ACCESSKEY="N"
>Volgende</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>References inside the constructor</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop.html"
ACCESSKEY="U"
>Omhoog</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Classes and Objects (PHP 5)</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>