Bug #67582 [NEW]: Cloned SplObjectStorage with overwritten getHash fails offsetExists()
| From: | m dot drewek at smf dot de | Date: | Mon, 07 Jul 2014 09:39:11 +0000 |
| Subject: | Bug #67582 [NEW]: Cloned SplObjectStorage with overwritten getHash fails offsetExists() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-186497@lists.php.net to get a copy of this message | ||
From: m dot drewek at smf dot de
Operating system: Irrelevant
PHP version: 5.5.14
Package: SPL related
Bug Type: Bug
Bug description:Cloned SplObjectStorage with overwritten getHash fails offsetExists()
Description:
------------
We use SplObjectStorage to store Entities, but overwrite getHash() to
prevent storing multiple objects with logically the same Entity (so "new
Entity(5)" should be considered equal to another "new Entity(5)"). This
works fine.
Now we considered that cloning such a list will make the offsetExists()
method fail, as the test script shows. As a consequence, removeAll()
will not remove anything, filter() will not filter anything, sort() will
just double the number of items contained, etc.
Test script:
---------------
class MyObjectStorage extends SplObjectStorage {
// Overwrite getHash() with just some (working) test-method
public function getHash($object) { return get_class($object); }
}
class TestObject {}
$list = new MyObjectStorage(); // No issues if using "new
SplObjectStorage()"
$list->attach(new TestObject());
foreach($list as $x) var_dump($list->offsetExists($x)); // TRUE
$list2 = clone $list;
foreach($list2 as $x) var_dump($list2->offsetExists($x)); // FALSE
Expected result:
----------------
Both the cloned and the original list return TRUE when calling
offsetExists() on a contained object.
Actual result:
--------------
Only the original list contains TRUE, while the cloned one returns
FALSE.
--
Edit bug report at https://bugs.php.net/bug.php?id=67582&edit=1
--