php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64643 MultipleIterator and its not scalar keys
Submitted: 2013-04-13 16:55 UTC Modified: 2013-04-13 17:36 UTC
From: mikolaj dot kopras at vexillium dot org Assigned: nikic (profile)
Status: Closed Package: SPL related
PHP Version: 5.4.14 OS: Ubuntu 2.6.38-15-generic x86_64
Private report: No CVE-ID: None
 [2013-04-13 16:55 UTC] mikolaj dot kopras at vexillium dot org
Description:
------------
The idea of MultipleIterator is great, however its key() method implementation is 
somehow invalid. Documentation of Iterator::key clearly stands, that key have to 
be a scalar or NULL, but MultipleIterator returns an array which leads to 
problems.

First problem is inability to use $key => $val syntax in foreach
Second problem is inability to use MultipleIterator with other iterators, e.g. 
with InfiniteIterator.

MultipleIterator could just compute key by imploding keys from all attached 
iterators using user specified separator. I've tried this localy and worked 
perfectly.
If that's the way to go, I'll be happy to help with patch.

Test script:
---------------
$multiIter = new MultipleIterator();
$multiIter->attachIterator(new ArrayIterator(array('a')));
$multiIter->attachIterator(new ArrayIterator(array('b')));

$infinityIter = new InfiniteIterator($multiIter);

foreach(new LimitIterator($infinityIter, 0, 2) as $item) {
    echo $item[0] . $item[1];
}

Expected result:
----------------
I expect "abab" to be printed

Actual result:
--------------
Warning: Illegal type returned from MultipleIterator::key() in 
/some/path/phptests/iterators/InfinityIterator.php on line 8
ab
Warning: Illegal type returned from MultipleIterator::key() in 
/some/path/phptests/iterators/InfinityIterator.php on line 8
ab
Warning: Illegal type returned from MultipleIterator::key() in 
/some/path/phptests/iterators/InfinityIterator.php on line 8


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-13 17:36 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2013-04-13 17:36 UTC] nikic@php.net
This issue is already fixed in PHP 5.5, where array keys will be allowed for iterators.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 23:01:32 2024 UTC