note 41417 added to language.oop.object-comparison-php4
| From: | jazfresh at hotmail dot com | Date: | Fri, 09 Apr 2004 01:44:41 +0000 |
| Subject: | note 41417 added to language.oop.object-comparison-php4 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67856@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop.object-comparison-php4.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41417
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41417&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41417&report=yes
Search -- http://master.php.net/manage/user-notes.php