note 41377 deleted from language.oop.object-comparison by danbrown

From: Date: Sat, 07 Nov 2009 19:01:49 +0000
Subject: note 41377 deleted from language.oop.object-comparison by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162550@lists.php.net to get a copy of this message
Note Submitter: jazfresh at hotmail dot com ---- PHP4 compares two objects (say, $a and $b) by comparing the type and all the attributes. If an attribute is an object, PHP4 will recursively call "if($a.attribute === $b.attribute)" to determine equality. However, this is a problem if a circular reference has been formed. The recursion will go on forever, and you will get the error message: "Fatal error: Nesting level too deep - recursive dependency?" Example, where the comparison will never terminate because PHP4 will forever recurse with comparisons of the attribute. <?php class Test { var $obj; } $foo =& new Test; $bar =& new Test; $foo->obj =& $bar; // Make circular reference $bar->obj =& $foo; if($foo === $bar) { baz(); } ?> First PHP4 does ($foo === $bar), which expands into ($foo.obj === $bar.obj), which expands into ($bar.obj === $foo.obj), and so on and so on. To avoid this situation, you must compare objects manually by comparing the two object's attributes and avoiding comparisons on attributes where a circular reference could arise. This issue is easily avoided in PHP5, where objects can be compared via references rather than the object contents.

« previous php.notes (#162550) next »