note 103125 added to language.oop5.object-comparison
| From: | d dot e dot pope at gmail dot com | Date: | Sun, 27 Mar 2011 20:27:12 +0000 |
| Subject: | note 103125 added to language.oop5.object-comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178252@lists.php.net to get a copy of this message | ||
Note that ALL object fields, public and private, are used in the equivalence check. If you want
public-only equivalence you'll have to roll your own.
<?php
function bool2str($bool)
{
if ($bool === false) {
return 'FALSE';
} else {
return 'TRUE';
}
}
class PubPrivFlag
{
public $pubFlag;
private $privFlag;
public function __construct($pub, $priv)
{
$this->pubFlag = $pub;
$this->privFlag = $priv;
}
}
echo "Two instances of the same class with identical private fields\n";
$s = new PubPrivFlag(true, true);
$t = new PubPrivFlag(true, true);
echo 'o1 == o2 : ' . bool2str($s == $t) . "\n";
echo "Two instances of the same class with different private fields\n";
$s = new PubPrivFlag(true, true);
$t = new PubPrivFlag(true, false);
echo 'o1 == o2 : ' . bool2str($s == $t) . "\n";
?>
This will output the following:
Two instances of the same class with identical private fields
o1 == o2 : TRUE
Two instances of the same class with different private fields
o1 == o2 : FALSE
----
Server IP: 69.147.83.197
Probable Submitter: 65.97.155.170
----
Manual Page -- http://www.php.net/manual/en/language.oop5.object-comparison.php
Edit -- https://master.php.net/note/edit/103125
Del: integrated -- https://master.php.net/note/delete/103125/integrated
Del: useless -- https://master.php.net/note/delete/103125/useless
Del: bad code -- https://master.php.net/note/delete/103125/bad+code
Del: spam -- https://master.php.net/note/delete/103125/spam
Del: non-english -- https://master.php.net/note/delete/103125/non-english
Del: in docs -- https://master.php.net/note/delete/103125/in+docs
Del: other reasons-- https://master.php.net/note/delete/103125
Reject -- https://master.php.net/note/reject/103125
Search -- https://master.php.net/manage/user-notes.php