note 41417 deleted from language.oop.object-comparison by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:01:47 +0000 |
| Subject: | note 41417 deleted from language.oop.object-comparison by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162549@lists.php.net to get a copy of this message | ||
Note Submitter: jazfresh at hotmail dot com
----
An addendum to the post below:
If you are comparing two objects and you know that they will be of the same type (you're just
not sure if they refer to the same object or not), then there is an easier and faster way to do the
comparison, and this also avoids the infinite recursion problem with circular references described
in the post below.
In the constructor of your object, set a "serial" attribute from a global variable that is
incremented whenever a new object is created. Then you just have to compare serial numbers to see if
the objects are the same.
e.g.
<?php
$FooSerial = 0;
class Foo {
var $serial;
function Foo() {
$this->serial = $GLOBALS['FooSerial']++;
// Rest of constructor...
}
// Rest of class definition...
}
if($bar->serial == $baz->serial) {
echo "Same";
}
?>