Bug #68682 [Opn->Nab]: RecursiveIteratorIterator does not recurse

From: Date: Fri, 22 Dec 2017 17:36:57 +0000
Subject: Bug #68682 [Opn->Nab]: RecursiveIteratorIterator does not recurse
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213249@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68682&edit=1 ID: 68682 Updated by: nikic@php.net Reported by: spinkus at epicmorsel dot com Summary: RecursiveIteratorIterator does not recurse -Status: Open +Status: Not a bug Type: Bug Package: SPL related Operating System: Linux PHP Version: 5.4.35 Block user comment: N Private report: N New Comment: As you already noticed yourself, RecursiveArrayIterator (despite the "array" in the name) also iterates over object properties. As such, the observed behavior is "expected". You can specify the CHILD_ARRAYS_ONLY flag to not iterate public properties, but in that case the object will not recurse at all. There is no option to recurse based on getIterator(). Previous Comments: ------------------------------------------------------------------------ [2014-12-29 10:35:41] spinkus at epicmorsel dot com Confirmed. In example data [1, 2, new X(), 5], the getIterator() method of the X instance is never called. ------------------------------------------------------------------------ [2014-12-29 10:31:12] spinkus at epicmorsel dot com Hmm, just realised it's probably more likely RecursiveIteratorIterator is treating the object in the array [1,2, new X(), 5] as an object and iterating its public fields. Investigating. ------------------------------------------------------------------------ [2014-12-29 10:08:26] spinkus at epicmorsel dot com Description: ------------ RecursiveIteratorIterator fails to Recurse into class implementing iteratorAggregate and returning RecursiveArrayIterator on private data. See test script. Test script: --------------- class X implements IteratorAggregate { private $data = ['X', 'X', 'X']; # public $data = ['X', 'X', 'X']; public function __construct($data = null) { $this->data = isset($data) ? $data : $this->data; } public function getIterator() { return new RecursiveArrayIterator($this->data); } } $x = new X([1, 2, new X(), 5]); $it = new RecursiveIteratorIterator($x); foreach($it as $v) { var_dump($v); } Expected result: ---------------- Expecting no difference between public and private data. RecursiveArrayIterator should make a copy of the private data on construction. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68682&edit=1

« previous php.bugs (#213249) next »