php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66286 Incorrect object comparison with inheritance
Submitted: 2013-12-13 15:09 UTC Modified: 2014-01-04 00:34 UTC
Votes:8
Avg. Score:4.5 ± 0.5
Reproduced:8 of 8 (100.0%)
Same Version:5 (62.5%)
Same OS:5 (62.5%)
From: m dot straczkowski at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.5.7 OS: Linux Ubuntu
Private report: No CVE-ID: None
 [2013-12-13 15:09 UTC] m dot straczkowski at gmail dot com
Description:
------------
Hello,

There is a problem with object comparing.
I can't see any logical reason for this to happen.

While comparing two different objects (with == operator) PHP returns true once and after var_dump / print_r (on objects) it returns false.

As PHP Manual said:

"Two object instances are equal if they have the same attributes and values, and are instances of the same class."

Our example two objects are not equal because their attribute values are different.

Script which shows this strange behaviour:
http://pastebin.com/nwEbe7sc

I'm using PHP 5.5.7-1+sury.org~saucy+1
Problem occurred on previous versions too

Test script:
---------------
http://pastebin.com/nwEbe7sc

Expected result:
----------------
bool(false)
B Object
(
    [errors:protected] => Array
        (
        )

    [value:protected] => ABC
)
B Object
(
    [errors:protected] => Array
        (
        )

    [value:protected] => CBA
)
bool(false)

Actual result:
--------------
bool(true)
B Object
(
    [errors:protected] => Array
        (
        )

    [value:protected] => ABC
)
B Object
(
    [errors:protected] => Array
        (
        )

    [value:protected] => CBA
)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-29 09:39 UTC] rkirilow at gmail dot com
I hit the same issue - this is a bug for sure.

Here are my findings:
 - PHP Version - 5.3 - no bug, 5.3+ the bug is there
 - OS - Linux Gentoo (not tested on other)
 - Observation/note: The issue seems to be related to that a property with same name ("_isSourceDirty" in my case and "errors" in previous example) is defined in both of the classes, no matter what the value is, if not the assertions does not fail. My example is extraction from Zend_CodeGenerator package (Zend Framework 1) however such usage is a very common case in typical inheritance usage
 - Workaround: Avoid usage of "loose" code, unless you want to shoot your self in the leg J

Test script:
---------------------------------------------
<?php
class Zend_CodeGenerator_Abstract
{
    protected $_isSourceDirty = true;
}

class Zend_CodeGenerator_Php_Abstract extends Zend_CodeGenerator_Abstract
{
    protected $_isSourceDirty = true;
}

class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
{
    protected $_shortDescription = null;
    
    public function setShortDescription($shortDescription)
    {
        $this->_shortDescription = $shortDescription;
        return $this;
    }
}

$d1 = new Zend_CodeGenerator_Php_Docblock();
$d1->setShortDescription('Short Desc One');

$d2 = new Zend_CodeGenerator_Php_Docblock();
$d2->setShortDescription('Short Desc Two');

// according to http://www.php.net/manual/en/language.oop5.object-comparison.php assertions should pass all

// strict comparison, expecting false
assert(false === ($d1 === $d2));

// loose comparison, expecting false
assert(false === ($d1 == $d2));

var_dump(array(
    'd1' => $d1,
    'd2' => $d2
));
 [2014-01-03 23:34 UTC] datibbaw@php.net
This changed since 5.4, as can be seen from this 3v4l:

http://3v4l.org/TJWQR
 [2014-01-04 00:32 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7e8e21df0c9aa39278e994b05540b69920201b32
Log: Fix bug #66286: Incorrect object comparison with inheritance
 [2014-01-04 00:32 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2014-01-04 00:34 UTC] nikic@php.net
-Summary: Comparing objects - very strange behaviour +Summary: Incorrect object comparison with inheritance -Assigned To: +Assigned To: nikic
 [2014-01-04 01:28 UTC] ab@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7e8e21df0c9aa39278e994b05540b69920201b32
Log: Fix bug #66286: Incorrect object comparison with inheritance
 [2014-10-07 23:16 UTC] stas@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=7e8e21df0c9aa39278e994b05540b69920201b32
Log: Fix bug #66286: Incorrect object comparison with inheritance
 [2014-10-07 23:27 UTC] stas@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=7e8e21df0c9aa39278e994b05540b69920201b32
Log: Fix bug #66286: Incorrect object comparison with inheritance
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC