php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64428 SPL RecursiveIteratorIterator::getChildren Uncaught exception
Submitted: 2013-03-15 09:40 UTC Modified: 2017-12-22 17:29 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: namarpi at yahoo dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.13 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: namarpi at yahoo dot com
New email:
PHP Version: OS:

 

 [2013-03-15 09:40 UTC] namarpi at yahoo dot com
Description:
------------
Recently I am working with SPL Iterators, and got an uncaught exception with the Test script.



Test script:
---------------
function example() {
    $array = array (
            'a' => array(
                    'a_1' => 'a 1 text',
            ),
    );

    $array_object = new ArrayObject( $array, 0, "RecursiveArrayIterator"  );
    $iterator     = new RecursiveIteratorIterator( $array_object->getIterator(),
                    RecursiveIteratorIterator::SELF_FIRST );

    foreach( $iterator as $key => $current ) {
var_dump( $iterator->getChildren );
var_dump( $iterator->getInnerIterator() );
        //if( $key == 'a' ) {
var_dump( $iterator->getInnerIterator()->getChildren() );
        //}
    }
}

example();

Expected result:
----------------
Smooth iteration through the leaves.

Actual result:
--------------
[15-Mar-2013 09:29:55 UTC] PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in [...]
Stack trace:
#0 [internal function]: ArrayIterator->__construct('a 1 text', 67108864)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-15 11:15 UTC] namarpi at yahoo dot com
Sorry, correctly this is about RecursiveArrayIterator::getChildren, and a more proper

Test script here:
---------------------------

function example() {
    $array = array (
            'a' => array(
                    'a_1' => 'a 1 text',
            ),
    );

    $array_object = new ArrayObject( $array, 0, "RecursiveArrayIterator"  );
    $iterator     = new RecursiveIteratorIterator( $array_object->getIterator(), RecursiveIteratorIterator::SELF_FIRST );

    foreach( $iterator as $key => $current ) {
var_dump( $iterator->getInnerIterator() );

        //if( $key == 'a' ) {
var_dump( get_class($iterator->getInnerIterator() ));
var_dump( $iterator->getInnerIterator()->getChildren() );
        //}
    }
}

example();
 [2013-03-15 16:28 UTC] laruence@php.net
getInnerIterator()->getChildren() => 'a 1 text'

why this is a bug?
 [2013-03-15 18:16 UTC] namarpi at yahoo dot com
I am using 14 Mar 2012, PHP 5.4.13, and running the test script without exception handling. This is the result:

[15-Mar-2013 18:06:44 UTC] RecursiveArrayIterator

[15-Mar-2013 18:06:44 UTC] PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in [...]
Stack trace:
#0 [internal function]: ArrayIterator->__construct('a 1 text', 67108864)
 [2013-03-15 18:23 UTC] namarpi at yahoo dot com
On the screen seems everything is OK, I can see the var_dump result also, but in the debug.log file there is a PHP error message.
 [2017-12-22 17:29 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-12-22 17:29 UTC] nikic@php.net
It is illegal to call getChildren() if hasChildren() is not true, as is the case here. The operation is ill-defined, and that's why you get an exception.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 01:01:33 2025 UTC