php 使用 arrayObject 进行致命异常处理

发布于 2024-10-18 06:56:34 字数 379 浏览 1 评论 0原文

需要有关 arrayObject 异常处理的帮助。我正在迭代一系列对象,但是当偏移结束时会发生致命异常。代码是:

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

错误是:

Fatal error: Uncaught exception 'InvalidArgumentException'

任何帮助都会很棒。

Need help with exception handling for arrayObject. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬心 2024-10-25 06:56:34

来自手册

The input parameter accepts an array or an Object.

现在正如@BoltClock所说:真的很难在不知道 b、c、q 和 num 的情况下弄清楚是什么,但如果 q 是一个数组,那么

$object_to_iterate = $q[$b];

可能只是一个字符串?然后

$obj = new ArrayObject ($object_to_iterate);

有一个不是对象或数组的参数?
也许对 $object_to_iterate 执行 var_dump(),并检查它是数组还是对象。

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.

咿呀咿呀哟 2024-10-25 06:56:34

$c 开头是什么?如果为 0(很可能),则 $b = -1,并且您试图获取 $q[-1],而该值很可能不存在。因此,您并没有真正将任何内容传递给 ArrayObject 构造函数。

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.

小苏打饼 2024-10-25 06:56:34

好的,我不知道这是否是最好的方法,但我使用了以下代码:

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 ++;
}

它可以通过

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文