Bug #64813 [Opn->Dup]: SimpleXMLIterator unintentionally rewinds to beginning

From: Date: Wed, 14 Apr 2021 13:35:11 +0000
Subject: Bug #64813 [Opn->Dup]: SimpleXMLIterator unintentionally rewinds to beginning
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233429@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64813&edit=1

 ID:                 64813
 Updated by:         imsop@php.net
 Reported by:        amfriedman at gmail dot com
 Summary:            SimpleXMLIterator unintentionally rewinds to
                     beginning
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            SimpleXML related
 Operating System:   Unbuntu 11
 PHP Version:        5.3.25
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of https://bugs.php.net/bug.php?id=55098


Previous Comments:
------------------------------------------------------------------------
[2013-05-10 17:24:25] 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.


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



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


Thread (2 messages)

« previous php.bugs (#233429) next »