Cloned SplObjectStorage with overwritten getHash fails offsetExists()

Bug #67582 Cloned SplObjectStorage with overwritten getHash fails offsetExists()
Submitted: 2014-07-07 09:39 UTC Modified: 2014-07-08 05:43 UTC
Votes:7
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

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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [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] nikic@php.net

-Status: Verified +Status: Closed