Bug #70418 [NEW]: SplObjectStorage not replacing objects when hash matches

From: Date: Thu, 03 Sep 2015 11:32:32 +0000
Subject: Bug #70418 [NEW]: SplObjectStorage not replacing objects when hash matches
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195733@lists.php.net to get a copy of this message
From: richardh at channelgrabber dot com Operating system: PHP version: 5.5.28 Package: SPL related Bug Type: Bug Bug description:SplObjectStorage not replacing objects when hash matches Description: ------------ If you change how the SplObjectStorage calculates an objects hash, and then attach an object which will create the same hash as a currently stored object, SplObjectStorage will silently drop the new object rather than replacing the current object at that hash with the new one. This means that if the object differs from the stored object, those changes are also dropped which can result in unexpected behaviour when iterating over the stored objects. This conflict with how arrays work when reassigning a value for the same index. Test script: --------------- class IdObjectStorage extends SplObjectStorage { public function getHash($object) { return (string) $object->id; } } $storage = new IdObjectStorage(); $array = []; $object1 = new stdClass(); $object1->id = 1; $object1->value = 'Object #1'; $storage->attach($object1); $array[$object1->id] = $object1; foreach($storage as $x) var_dump($x->value); // Object #1 foreach($array as $x) var_dump($x->value); // Object #1 $object2 = new stdClass(); $object2->id = 1; $object2->value = 'Object #2'; $array[$object2->id] = $object2; $storage->attach($object2); foreach($storage as $x) var_dump($x->value); // Object #1 foreach($array as $x) var_dump($x->value); // Object #2 Expected result: ---------------- SplObjectStorage should work the same as arrays, in that when an object is attached with the same hash as a current entry, that entry is replaced with the new object. string(9) "Object #1" string(9) "Object #1" string(9) "Object #2" string(9) "Object #2" Actual result: -------------- SplObjectStorage silently drops the new object as it already has an object for that hash. string(9) "Object #1" string(9) "Object #1" string(9) "Object #1" string(9) "Object #2" -- Edit bug report at https://bugs.php.net/bug.php?id=70418&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70418&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70418&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70418&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70418&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70418&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70418&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70418&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70418&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70418&r=support Expected behavior: https://bugs.php.net/fix.php?id=70418&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70418&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70418&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70418&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70418&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70418&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70418&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70418&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70418&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70418&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70418&r=mysqlcfg

« previous php.bugs (#195733) next »