|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67582 Cloned SplObjectStorage with overwritten getHash fails offsetExists()
Submitted: 2014-07-07 09:39 UTC Modified: 2014-07-08 05:43 UTC
Avg. Score:4.3 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:5 (71.4%)
From: m dot drewek at smf dot de Assigned:
Status: Closed Package: SPL related
PHP Version: 5.5.14 OS: Irrelevant
Private report: No CVE-ID: None
 [2014-07-07 09:39 UTC] m dot drewek at smf dot de
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-08 05:03 UTC]
It seems it's broken since 5.4.0.
 [2014-07-08 05:43 UTC]
-Status: Open +Status: Verified
 [2015-07-08 12:12 UTC] nospampls at example dot org
it is broken since 5.4 because getHash() was introduced since php 5.4
fix this please!
 [2016-03-20 16:49 UTC]
Automatic comment on behalf of nikic
Log: Fixed bug #67582
 [2016-03-20 16:49 UTC]
-Status: Verified +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 08:01:31 2024 UTC