|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 08:00:02 2025 UTC |
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 ));