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: -
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: Open Package: SimpleXML related
PHP Version: 5.3.25 OS: Unbuntu 11
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: 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

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 23:01:27 2019 UTC