php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44411 Broken Compatibility
Submitted: 2008-03-12 01:35 UTC Modified: 2008-11-07 11:01 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: phpbugs at steve dot ipapp dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: ANY
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugs at steve dot ipapp dot com
New email:
PHP Version: OS:

 

 [2008-03-12 01:35 UTC] phpbugs at steve dot ipapp dot com
Description:
------------
In PHP 5.0.x and 5.1.x two SimpleXMLElement objects were considered equal if they represented the same data.

In PHP 5.2.x this does not seem to be the case anymore. This was previously listed as bug 39866 [http://bugs.php.net/bug.php?id=39866], but for some reason this was listed as bogus.  In that bug it was noted that we should look at Example 5 at http://php.net/simplexml. I'm assuming this is Example 6 now [Comparing Elements and Attributes with Text] , but this is incorrect as comparision will implicity cast 
it to string anyway.

According to http://www.php.net/manual/en/language.oop5.object-comparison.php:
"When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class... On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class." 

Furthermore this backwards incompatible change is not listed in : http://us.php.net/manual/en/migration52.incompatible.php.

Currently there is no equals method that we can call to get the previous functionality back, and at the present moment this makes == no longer transitive as  especially since this makes == no longer transitive as 'doc' == x1, 'doc' == x2, but x1 != x2. 

I do not understand why ==, nor do I see value in, doing a strict comparison for SimpleXMLObjects as ==, when according to the PHP Object Comparison manual this should be ===. 

Therefore I believe this is a bug.




Reproduce code:
---------------
$xmldoc = "<xml><value>foo</value></xml>";

$sax1 = new SimpleXMLElement("$xmldoc");
$sax2 = new SimpleXMLElement("$xmldoc");

if($sax1 == $sax2)
{
        echo "TRUE";
} else
{
        echo "FALSE";
}
echo "\n\n\n";


Expected result:
----------------
TRUE

Actual result:
--------------
FALSE

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-12 04:41 UTC] hubert dot roksor at gmail dot com
As per the manual chapter you quoted, the comparison operator compares _objects_, and that's the key word here. You expect two equal XML trees to be represented by two equal objects, and even though this expectation is understandable, it is simply not the case. Those objects are different. In fact, even if $sax1->value is equal to $sax1->value and they come from the same tree, they are not identical. ($sax1->value !== $sax1->value)

The bottom line is the comparison operator compares objects. If you need an operator that understands the underlying data you will have to use another mean. The solution proposed in the other bug report (compare them as XML strings) sounds reasonable.

It would be nice to mention this quirk in the manual though, perhaps as a new example? "Comparing Elements and Elements"
 [2008-03-12 09:43 UTC] phpbugs at steve dot ipapp dot com
But then what is the difference between strict comparision and loose comparison for SimpleXMLObjects? 

I'm then also unclear as to what exactly == was doing prior to 5.2.0. It seemed to convey the idea atleast to me and one other person that the classical object equality of 'if all the members are equal' seemed to work. Furthermore if this functionality was not buggy or superious would there be some way to have another method, perhaps equals() that achieves this?
 [2008-03-13 17:08 UTC] phpbugs at steve dot ipapp dot com
Sorry,

Finally one more question, 

I looked over the Change Log, and don't see any reference to this change in behaivor either:
http://www.php.net/ChangeLog-5.php#5.2.5
 [2008-07-25 14:27 UTC] paul at preinheimer dot com
Small Reference; http://screencast.com/t/mNbCoaVb1
 [2008-11-07 11:01 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I've added a new example.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 17:01:34 2025 UTC