|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-23 10:08 UTC] an0nym at narod dot ru
Description:
------------
Iteration over SplObjectStorage returns unexpected results: internal index => associated object instead of object => associated data.
Reproduce code:
---------------
<?php
$spl = new SplObjectStorage ();
$spl[new StdClass ()] = 'a';
$spl[new StdClass ()] = 'b';
foreach ($spl as $object => $data)
{
var_dump ($object, $data);
}
?>
Expected result:
----------------
object(stdClass)#2 (0) {
}
string(1) "a"
object(stdClass)#3 (0) {
}
string(1) "b"
Actual result:
--------------
int(0)
object(stdClass)#2 (0) {
}
int(1)
object(stdClass)#2 (0) {
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 02:00:01 2025 UTC |
Ah, it seems that foreach can't handle anything non-scalar as a key. What a pity. I think this should be changed in the future, because it limits the use of Iterators. For instance, I can't use something like: <?php class MapObjectData extends SplObjectStorage { public function key () { return parent::current (); } public function current () { return parent::getInfo (); } } $spl = new MapObjectData (); $spl[new StdClass ()] = 'a'; $spl[new StdClass ()] = 'b'; foreach ($spl as $object => $data) { var_dump ($object, $data); } ?>