php8 -splfixedArray迭代器的next()
请看一下此代码:
<?php
$array = new SplFixedArray(1);
print "\nKey1: " . $array->getIterator()->key();
$array->setSize(5);
$array->getIterator()->next();
$array->getIterator()->next();
$array->getIterator()->next();
print "\nKey2: " . $array->getIterator()->key() . "\n";
/*
Result (PHPv8.1.5):
Key1: 0
Key2: 0
*/
我对,在这种情况下,key1和key2不应相同(0),而是从0(key1)移动到3(key2)? 在PHP8中,使用SplfixedArray,他们被迫使用迭代器,因此现在已嵌套了循环安全。但是通过 - &gt; next()迭代似乎……不是要迭代。因此,指针仍处于位置0。
是PHP8的错误还是我的代码错误?
谢谢。
Please take a look at this code:
<?php
$array = new SplFixedArray(1);
print "\nKey1: " . $array->getIterator()->key();
$array->setSize(5);
$array->getIterator()->next();
$array->getIterator()->next();
$array->getIterator()->next();
print "\nKey2: " . $array->getIterator()->key() . "\n";
/*
Result (PHPv8.1.5):
Key1: 0
Key2: 0
*/
Am I right that in this case Key1 and Key2 shouldn't be the same (0) but moved from 0 (key1) to 3 (key2)?
In PHP8 using splFixedArray they forced to use iterator so it's now nested loops-safe. But iterating by ->next() seems... not to iterate. So the pointer is still at the position 0.
Is it PHP8's bug or my code is wrong?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是PHP一边的任何错误。它可以按预期工作。问题是您的代码。
上线将始终返回迭代器的新实例。每次都不会遵循任何单身模式来返回同一实例。因此,如果您希望使用迭代器移动内部指针并打印值,请在变量中收集实例,然后使用相同的变量调用
next()
每次如下:在线演示
It is not any bug from PHP's side. It works as expected. The issue is with your code.
The above line will always return a new instance of the iterator. It doesn't follow any singleton pattern to return the same instance each time. So, if you wish to move the internal pointers with iterators and print values, collect the instance in a variable and use the same variable to call
next()
each time like below:Online Demo