Bug #66286 [Opn]: Comparing objects - very strange behaviour
| From: | datibbaw@php.net | Date: | Fri, 03 Jan 2014 23:34:45 +0000 |
| Subject: | Bug #66286 [Opn]: Comparing objects - very strange behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183576@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66286&edit=1
ID: 66286
Updated by: datibbaw@php.net
Reported by: m dot straczkowski at gmail dot com
Summary: Comparing objects - very strange behaviour
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Linux Ubuntu
PHP Version: 5.5.7
Block user comment: N
Private report: N
New Comment:
This changed since 5.4, as can be seen from this 3v4l:
http://3v4l.org/TJWQR
Previous Comments:
------------------------------------------------------------------------
[2013-12-29 09:39:19] 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
));
------------------------------------------------------------------------
[2013-12-13 15:09:25] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66286&edit=1