开发者

php fatal exception handling with arrayObject

Need help with exception handling for arra开发者_如何学编程yObject. I'm iterating through a series of objects but when the offset ends fatal exception occurs. The code is:

while ($c <= 10) {
    $num = 1;
    $b = $c-$num;
    $object_to_iterate = $q[$b];
    $obj = new ArrayObject ($object_to_iterate);
    iterateObject($obj);
    $c ++;
}

The error is:

Fatal error: Uncaught exception 'InvalidArgumentException'

Any help would be great.


from the manual:

The input parameter accepts an array or an Object.

Now as @BoltClock said: it is really hard to figure out without knowing what b, c, q and num are, but if q is an array, then

$object_to_iterate = $q[$b];

might be just a string? And then

$obj = new ArrayObject ($object_to_iterate);

has an argument that is not an object or array? Maybe do a var_dump() on that $object_to_iterate, and check is it is an array or object.


What does $c start as? If 0 (which is likely), then $b = -1 and you're trying to get $q[-1], which, again, likely doesn't exist. So you're not really passing anything to the ArrayObject constructor.


OK I don't know if this is the best way to do it but I used the following code:

while ($c <= 10) {
$num = 1;
$b = $c-$num;
$object_to_iterate = $q[$b];
//exception handling
if (empty($q[$b])) {
break;
} else {
$obj = new ArrayObject($object_to_iterate);
iterateObject($obj); 
}
$c ++;
}

It works through

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜