php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49967 Custom iteration cannot return keys other than ints or strings
Submitted: 2009-10-23 10:08 UTC Modified: 2013-09-21 23:17 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: an0nym at narod dot ru Assigned: colder (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
24 + 38 = ?
Subscribe to this entry?

 
 [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) {
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-23 10:27 UTC] an0nym at narod dot ru
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);
}
?>
 [2009-11-11 12:13 UTC] colder@php.net
This is indeed a limitation of the iteration process itself, nothing specific to splobjectstorage.
 [2010-12-20 09:21 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2012-06-05 23:21 UTC] nikic@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: colder
 [2013-09-21 23:17 UTC] levim@php.net
-Status: Assigned +Status: Closed
 [2013-09-21 23:17 UTC] levim@php.net
As of PHP 5.5, this is now fixed.
 [2014-01-06 23:29 UTC] haircut at gmail dot com
This still repros as originally submitted for me with PHP 5.5.3 (on my Ubuntu 13.10 64-bit desktop).
 [2014-01-07 06:12 UTC] nikic@php.net
@haircut: Only the underlying feature (returning non-str/int keys from iterators) has been implemented, the SplObjectStorage behavior had to be kept for BC reasons.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC