#48205 [Bgs]: RecursiveIteratorIterator->callGetChildren() raises InvalidArgumentException
| From: | sean at practicalweb dot co dot uk | Date: | Sun, 10 May 2009 09:55:34 +0000 |
| Subject: | #48205 [Bgs]: RecursiveIteratorIterator->callGetChildren() raises InvalidArgumentException | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-137097@lists.php.net to get a copy of this message | ||
ID: 48205
User updated by: sean at practicalweb dot co dot uk
Reported By: sean at practicalweb dot co dot uk
Status: Bogus
Bug Type: SPL related
Operating System: *
PHP Version: 5.*, 6CVS (2009-05-09)
New Comment:
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" ?
Previous Comments:
------------------------------------------------------------------------
[2009-05-09 20:36:19] 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-09 19:18:42] 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 13:52:57] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48205&edit=1