php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48205 RecursiveIteratorIterator->callGetChildren() raises InvalidArgumentException
Submitted: 2009-05-09 13:52 UTC Modified: 2009-05-10 09:55 UTC
From: sean at practicalweb dot co dot uk Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.*, 6CVS (2009-05-09) OS: *
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: sean at practicalweb dot co dot uk
New email:
PHP Version: OS:

 

 [2009-05-09 13:52 UTC] sean at practicalweb dot co dot uk
Description:
------------
If the current element of a RecursiveIteratorIterator has no children then callGetChildren() causes an error message like

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' 

This is confusing because the PHP code does not pass an argument here.

I think that either the method should return null when there are no children - or at least the error message could be clearer.

This has been found as part of phptestfest 09 - an XFail test will be commited from PHPLondon

My Apologies if this is expected behaviour, I realise the code is undocumented, but I wanted to commit a test to cover this and filing a bug seemed the best way to confirm one way or the other :-)

Reproduce code:
---------------
<?php
  $array = array(array(7,8,9),1,2,3,array(4,5,6));
$recursiveArrayIterator = new RecursiveArrayIterator($array);
$test = new RecursiveIteratorIterator($recursiveArrayIterator);

var_dump($test->current());
$test->next();
var_dump($test->current());
try {
  $output = $test->callGetChildren();
} catch (InvalidArgumentException $ilae){
  $output = null;  
  print "invalid argument exception\n";
}
var_dump($output);
?>

Expected result:
----------------
array(3) {
  [0]=>
  int(7)
  [1]=>
  int(8)
  [2]=>
  int(9)
}
int(7)
NULL

Actual result:
--------------
array(3) {
  [0]=>
  int(7)
  [1]=>
  int(8)
  [2]=>
  int(9)
}
int(7)
invalid argument exception
NULL


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-09 19:18 UTC] jani@php.net
Please, when you do testing, test with PHP_5_2 also and HEAD. And set 
the version string like I did now in case the bug exists in all 
branches..
 [2009-05-09 20:36 UTC] colder@php.net
That's expected, callGetChildren will call RecursiveArrayIterator::getChildren when the current element is an integer. getChildren will then instantiate an ArrayIterator object, and pass the current element as argument, resulting in an exception since it's not an array or an object.
 [2009-05-10 09:55 UTC] sean at practicalweb dot co dot uk
Hi,
   I should have included the error message in the bug report.

It reads:

"Passed variable is not an array or object, using empty array instead"

I can understand throwing an exception here, but neither the type of exception nor message make much sense to me.

The error is cause by using callGetChildren() on an item that has no children - but the exception and message are about invalid arguments.

I can see how this happens from the php source - but it doesn't helpfully reflect the error the user has made.

Would it be possible to have a new class of error and a message that says something like "getChildren called on an object with no children" ?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 06:01:28 2024 UTC