php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60762 IteratorIterator doesn't iterate over DomNodeList
Submitted: 2012-01-15 14:59 UTC Modified: 2021-07-06 13:53 UTC
From: jthijssen at noxlogic dot nl Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 5.4.0RC5 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jthijssen at noxlogic dot nl
New email:
PHP Version: OS:

 

 [2012-01-15 14:59 UTC] jthijssen at noxlogic dot nl
Description:
------------
When creating an IteratorIterator from a DomNodeList, it doesn't behave correctly.  The current example behaves like the iterator-key is not reset properly when iterating although the value returned is indeed the first entry of the iterator.


Test script:
---------------
$XML = <<< XML
<root>
  <item>1</item>
  <item>2</item>
  <item>3</item>
</root>
XML;

$dom = new DomDocument();
$dom->loadXml($XML);
$items = $dom->getElementsByTagName('item');

foreach ($items as $k => $item) {
	print "K: ".$k."  Item: ".$item->nodeName. " Value: ".$item->nodeValue. PHP_EOL;
}

print "----" . PHP_EOL;
$it = new IteratorIterator($items);
foreach ($it as $k => $item) {
	print "K: ".$k."  Item: ".$item->nodeName. " Value: ".$item->nodeValue. PHP_EOL;
}

Expected result:
----------------
K: 0  Item: item Value: 1
K: 1  Item: item Value: 2
K: 2  Item: item Value: 3
----
K: 0  Item: item Value: 1
K: 1  Item: item Value: 2
K: 2  Item: item Value: 3

Actual result:
--------------
K: 0  Item: item Value: 1
K: 1  Item: item Value: 2
K: 2  Item: item Value: 3
----
K: 3  Item: item Value: 1


Patches

set-index (last revision 2018-08-19 15:27 UTC by cmb@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-04 16:11 UTC] colder@php.net
-Assigned To: +Assigned To: colder
 [2017-10-24 07:25 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: colder +Assigned To:
 [2018-08-19 15:27 UTC] cmb@php.net
The following patch has been added/updated:

Patch Name: set-index
Revision:   1534692427
URL:        https://bugs.php.net/patch-display.php?bug=60762&patch=set-index&revision=1534692427
 [2018-08-19 15:27 UTC] cmb@php.net
Confirmed: <https://3v4l.org/IGWHd>.  It get's worse without the
first iteration, since we'd run into an infinite loop then.  The
problem is that moving forward relies on the index member of
zend_object_iterator[1], which is documented to be private, and is
not set from outer iterators.  If we're willing to disregard this
privacy further, the attached set-index patch is supposed to solve
the issue.

[1] <https://github.com/php/php-src/blob/php-7.3.0beta1/Zend/zend_iterators.h#L59>
 [2018-08-19 15:45 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-07-06 13:53 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-07-06 13:53 UTC] cmb@php.net
This issue is fixed as of PHP 8.0.0[1]; I suggest to leave it at
this.

[1] <https://3v4l.org/IGWHd>
 [2021-07-06 13:53 UTC] cmb@php.net
This issue is fixed as of PHP 8.0.0[1]; I suggest to leave it at
this.

[1] <https://3v4l.org/IGWHd>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC