我正在试图了解FilterIterator在本守则的行为,我试图理解动作序列,我不明白为什么如果您试图打印current()值,它将无法工作,除非您以前使用next()或rewind():
// Please take a look at the link before
echo $cull->current(); // wont work
$cull->next(); or $cull->rewind(); then echo $cull->current(); // work现在我不知道我有什么需要“刷新”的“指针”才能打印元素,如果有人能向我解释一下动作序列马比它会变得更清楚,谢谢大家,祝您愉快的一天。
发布于 2012-09-17 06:58:21
如果在第一次访问next()或rewind之前不调用current(),则内部迭代器指针不会设置为第一个元素.
常见的场景是while($it->next())!
发布于 2012-10-14 21:42:45
这是我在这里问的同一个问题,尽管它听起来不一样:为什么我必须倒带IteratorIterator (您的CullingIterator是一个FilterIterator,它是一个IteratorIterator)。
请阅读已接受的答案和注释,但总结是IteratorIterator是用php源代码编写的,在功能上模拟如下所示:
class IteratorIterator {
private $cachedCurrentValue;
private $innerIterator;
...
public function current() { return $this->cachedCurrentValue; }
public function next() {
$this->innerIterator->next();
$this->cachedCurrentValue = $this->innerIterator->current();
}
public function rewind() {
$this->innerIterator->rewind();
$this->cachedCurrentValue = $this->innerIterator->current();
}
}重要的是,在调用current()时,不从内部迭代器中检索值,而是在其他时间检索值(构造函数不是其中之一)。
就我个人而言,我认为这是一个边缘错误,因为它是意外的和可以解决的,而不引入不必要的行为或性能问题,但哦,好吧。
https://stackoverflow.com/questions/12454368
复制相似问题