note 41377 added to language.oop.object-comparison-php4
| From: | jazfresh at hotmail dot com | Date: | Thu, 08 Apr 2004 01:18:12 +0000 |
| Subject: | note 41377 added to language.oop.object-comparison-php4 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67793@lists.php.net to get a copy of this message | ||
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.
----
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+41377
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41377&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41377&report=yes
Search -- http://master.php.net/manage/user-notes.php