Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php、Spl、Filtererator行为_Php_Spl_Filter Iterator - Fatal编程技术网

Php、Spl、Filtererator行为

Php、Spl、Filtererator行为,php,spl,filter-iterator,Php,Spl,Filter Iterator,我试图了解过滤器操作员的行为, 我试图理解动作序列,我不明白为什么如果您尝试打印当前()值,它将无法工作,除非您在之前使用下一步()或倒带(),例如: // Please take a look at the link before echo $cull->current(); // wont work $cull->next(); or $cull->rewind(); then echo $cull->current(); // work 现在我不知道我必须“刷新”

我试图了解过滤器操作员的行为, 我试图理解动作序列,我不明白为什么如果您尝试打印当前()值,它将无法工作,除非您在之前使用下一步()倒带(),例如:

// Please take a look at the link before
echo $cull->current(); // wont work
$cull->next(); or $cull->rewind(); then echo $cull->current(); // work

现在我不知道我必须“刷新”什么“指针”才能打印元素,如果有人能给我解释一下动作顺序,也许它会变得更清晰,谢谢大家,祝你们过得愉快。

如果在第一次访问
current()
之前不调用
next()
倒带
,我该怎么办,内部迭代器指针未设置为第一个元素


常见的场景是
while($it->next())
AFAIK

这是我在这里问的同一个问题,尽管听起来不同: (您的CullingIterator是一个FilterIterator,它是一个迭代器)

阅读已接受的答案和评论,但总结是,迭代器在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()时,不会从内部迭代器检索值,而是在其他时间检索值(构造函数不是其中之一)


就我个人而言,我认为这是一个边界错误,因为它是意外的,可以解决,不会引入不必要的行为或性能问题,但哦,好吧

你能在链接上解释一下我的例子中的动作序列吗?你为什么链接到一个外部网站来获取你的代码?强烈建议在此处发布代码。