Req #49967 [Com]: Custom iteration cannot return keys other than ints or strings

From: Date: Tue, 07 Jan 2014 06:12:46 +0000
Subject: Req #49967 [Com]: Custom iteration cannot return keys other than ints or strings
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183621@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49967&edit=1

 ID:                 49967
 Comment by:         nikic@php.net
 Reported by:        an0nym at narod dot ru
 Summary:            Custom iteration cannot return keys other than ints
                     or strings
 Status:             Closed
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.3.0
 Assigned To:        colder
 Block user comment: N
 Private report:     N

 New Comment:

@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.


Previous Comments:
------------------------------------------------------------------------
[2014-01-06 23:29:52] 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).

------------------------------------------------------------------------
[2013-09-21 23:17:55] levim@php.net

As of PHP 5.5, this is now fixed.

------------------------------------------------------------------------
[2009-11-11 12:13:28] colder@php.net

This is indeed a limitation of the iteration process itself, nothing specific to splobjectstorage.

------------------------------------------------------------------------
[2009-10-23 10:27:31] 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-10-23 10:08:10] 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) {
}


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=49967&edit=1


Thread (7 messages)

« previous php.bugs (#183621) next »