php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64813 SimpleXMLIterator unintentionally rewinds to beginning
Submitted: 2013-05-10 17:24 UTC Modified: 2021-04-14 13:35 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: amfriedman at gmail dot com Assigned:
Status: Duplicate Package: SimpleXML related
PHP Version: 5.3.25 OS: Unbuntu 11
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: amfriedman at gmail dot com
New email:
PHP Version: OS:

 

 [2013-05-10 17:24 UTC] amfriedman at gmail dot com
Description:
------------
---
From manual page: http://php.net/manual/en/class.simplexmliterator.php
---


When under a for-loop and iterating through a SimpleXMLIterator object, merely 
reading from a variable that stores the children being iterated will cause a 
rewind to the beginning of the object within which you are looping.

Test script:
---------------
$xml = new SimpleXMLIterator($xml_str);

$nodes = $nodes_for_lookup = $xml->children();

$n_node = -1;
		
for($nodes->rewind(); $nodes->valid(); $nodes->next()) {
  $node = $nodes->current();
  $n_node++;

  // This rewinds this loop
  $value_to_check = $nodes[$n_node+1];
  
  // This also rewinds the loop
  $value_to_check = $nodes_for_lookup[$n_node+1];

  // This does NOT rewind the loop (expected behavior)
  $nodes_now = $xml->children();
  $value_to_check = $nodes_now[$n_node+1];
}
						

Expected result:
----------------
The loop should NOT be rewound!  Merely reading from the object should not affect 
the iterator's pointer whatsoever.

Actual result:
--------------
The loop is getting rewound due to reading a specific key-based item in the 
iterator object/array.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-14 13:35 UTC] imsop@php.net
-Status: Open +Status: Duplicate
 [2021-04-14 13:35 UTC] imsop@php.net
Duplicate of https://bugs.php.net/bug.php?id=55098
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC