2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.oop5.object-comparison" xmlns="http://docbook.org/ns/docbook">
|
2009-09-10 05:07:22 +00:00
|
|
|
<title>Comparing Objects</title>
|
2004-07-11 09:32:52 +00:00
|
|
|
<para>
|
|
|
|
When using the comparison operator (<literal>==</literal>),
|
|
|
|
object variables are compared in a simple manner, namely: Two object
|
2016-09-28 14:19:56 +00:00
|
|
|
instances are equal if they have the same attributes and values (values are compared with <literal>==</literal>), and are
|
2004-07-11 09:32:52 +00:00
|
|
|
instances of the same class.
|
|
|
|
</para>
|
|
|
|
<para>
|
2014-02-23 08:22:12 +00:00
|
|
|
When using the identity operator (<literal>===</literal>),
|
2004-07-11 09:32:52 +00:00
|
|
|
object variables are identical if and only if they refer to the same
|
|
|
|
instance of the same class.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
An example will clarify these rules.
|
|
|
|
<example>
|
2020-12-15 01:40:52 +00:00
|
|
|
<title>Example of object comparison</title>
|
2011-09-30 14:21:51 +00:00
|
|
|
<programlisting role="php">
|
2004-07-11 09:32:52 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
function bool2str($bool)
|
|
|
|
{
|
2004-07-11 09:32:52 +00:00
|
|
|
if ($bool === false) {
|
2004-10-02 09:40:52 +00:00
|
|
|
return 'FALSE';
|
2004-07-11 09:32:52 +00:00
|
|
|
} else {
|
2004-10-02 09:40:52 +00:00
|
|
|
return 'TRUE';
|
2004-07-11 09:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-02 09:40:52 +00:00
|
|
|
function compareObjects(&$o1, &$o2)
|
|
|
|
{
|
|
|
|
echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
|
|
|
|
echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n";
|
|
|
|
echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n";
|
|
|
|
echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n";
|
2004-07-11 09:32:52 +00:00
|
|
|
}
|
|
|
|
|
2004-10-02 09:40:52 +00:00
|
|
|
class Flag
|
|
|
|
{
|
|
|
|
public $flag;
|
2004-07-11 09:32:52 +00:00
|
|
|
|
2017-10-28 08:25:00 +00:00
|
|
|
function __construct($flag = true) {
|
2004-10-02 09:40:52 +00:00
|
|
|
$this->flag = $flag;
|
2004-07-11 09:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-02 09:40:52 +00:00
|
|
|
class OtherFlag
|
|
|
|
{
|
|
|
|
public $flag;
|
2004-07-11 09:32:52 +00:00
|
|
|
|
2017-10-28 08:25:00 +00:00
|
|
|
function __construct($flag = true) {
|
2004-10-02 09:40:52 +00:00
|
|
|
$this->flag = $flag;
|
2004-07-11 09:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$o = new Flag();
|
|
|
|
$p = new Flag();
|
|
|
|
$q = $o;
|
|
|
|
$r = new OtherFlag();
|
|
|
|
|
|
|
|
echo "Two instances of the same class\n";
|
|
|
|
compareObjects($o, $p);
|
|
|
|
|
|
|
|
echo "\nTwo references to the same instance\n";
|
|
|
|
compareObjects($o, $q);
|
|
|
|
|
|
|
|
echo "\nInstances of two different classes\n";
|
|
|
|
compareObjects($o, $r);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2005-01-23 20:04:36 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2004-07-11 09:32:52 +00:00
|
|
|
Two instances of the same class
|
|
|
|
o1 == o2 : TRUE
|
|
|
|
o1 != o2 : FALSE
|
|
|
|
o1 === o2 : FALSE
|
|
|
|
o1 !== o2 : TRUE
|
|
|
|
|
|
|
|
Two references to the same instance
|
|
|
|
o1 == o2 : TRUE
|
|
|
|
o1 != o2 : FALSE
|
|
|
|
o1 === o2 : TRUE
|
|
|
|
o1 !== o2 : FALSE
|
|
|
|
|
|
|
|
Instances of two different classes
|
|
|
|
o1 == o2 : FALSE
|
|
|
|
o1 != o2 : TRUE
|
|
|
|
o1 === o2 : FALSE
|
|
|
|
o1 !== o2 : TRUE
|
2005-01-23 20:04:36 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
2004-07-11 09:32:52 +00:00
|
|
|
</para>
|
2007-08-17 02:14:00 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2013-08-08 02:18:54 +00:00
|
|
|
Extensions can define own rules for their objects comparison
|
|
|
|
(<literal>==</literal>).
|
2007-08-17 02:14:00 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2004-07-11 09:32:52 +00:00
|
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-07-11 09:32:52 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|