php.net |  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
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:
 [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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-08 05:03 UTC] yohgaki@php.net
http://3v4l.org/Kc9u1
It seems it's broken since 5.4.0.
 [2014-07-08 05:43 UTC] yohgaki@php.net
-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] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cc3cdd00578006a5684e6dfaf81532a13326b9fe
Log: Fixed bug #67582
 [2016-03-20 16:49 UTC] nikic@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Feb 26 21:01:35 2017 UTC