PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #35053 === is wrong for objects and also stupid
Submitted:1 Nov 2005 5:02pm UTC Modified: 1 Nov 2005 5:30pm UTC
From:sam at liddicott dot com Assigned to:
Status:Wont fix Category:Scripting Engine problem
Version:4.4.0 OS:Linux
Votes:17 Avg. Score:4.4 ± 1.0 Reproduced:15 of 16 (93.8%)
Same Version:6 (40.0%) Same OS:9 (60.0%)
View/Vote Developer Edit Submission

Have you experienced this issue?
Rate the importance of this bug to you:

[1 Nov 2005 5:02pm UTC] sam at liddicott dot com
Description:
------------
=== does not compare two objects for identity, it compares the values of
their fields, and recursively at that! It gives wrong answers and in
some cases fails altogether!

Would you believe that === reports two different objects as being the
same object, when they are not references?

Why can't it just check if they have the same storage address, that
should be quick, painless and accurate!

There should be some way to tell when two object references refer to the
same object (apart hack in an extra field and see if it appears in the
other)

Reproduce code:
---------------
<?php

$t->name='t';
$e->name='e';

$t->e=&$e;
$e->t=&$t;

$tt=&$t;

$a->n='n';
$b->n='n';

if ($a===$b) print "Dear me A and B are the same\n";
else print "Of course A and B are different\n";

if ($tt===$t) print "Yes, of course TT and T are the same\n";
else print "No they are not\n";
?>

Expected result:
----------------
"Of course A and B are different"
"Yes, of course TT and T are the same"

Actual result:
--------------
Dear me A and B are the same

Fatal error: Nesting level too deep - recursive dependency? in
/home/sam/projects/reporter/t.php on line 17

[1 Nov 2005 5:06pm UTC] derick@php.net
We can not change this behavior, as this might break existing scripts.
[1 Nov 2005 5:30pm UTC] sam at liddicott dot com
Well why not add a new operator ==== that does the job properly?

Or a function getAddressOfObject (a 5 liner, surely) that returns the
address of the object so a numeric compare can be done, or something
that does the job that needs doing?

Sam

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC