Comparison of class with recursive references causes fatal error
| From: | Mark Evans | Date: | Sat, 15 Oct 2005 10:48:58 +0000 |
| Subject: | Comparison of class with recursive references causes fatal error | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-224180@lists.php.net to get a copy of this message | ||
Hi All
I am coming up against a similar issue as
http://bugs.php.net/bug.php?id=31449
Take the following code for example (from the bug report)
<?php
class Recursive {
private $me;
function __construct() {
$this->me = $this;
}
}
new Recursive() != new Recursive();?> A "nesting level too deep" fatal error is thrown... I can kind of see why this happens... The bug report is closed as bogus with the following statement "I don't think it's a bug. You can not compare these objects since they contain circular references, so comparison process descends to the endless loop. You may want to use custom method to compare objects - bit-comparison of objects is generally not the best idea." the question is how to solve this... I couldnt see any immediate way to comare 2 objects easily.. Some idea's I came up with... 1. Serialize the 2 objects and then compare the serialized data. 2. Compare the 2 classes using get_class_methods() and get_class_vars() Is there an easier way? Or has anyone got any better suggestions that I can use? Note: the above code is for PHP 5.. I am using PHP 4 and come up against a similar issue when using mock objects to unit test an observer pattern. Regards Mark