PHP RecursiveIterator traversing

丶灬走出姿态 提交于 2019-12-05 03:31:51

To iterate over a RecursiveIterator, you have to wrap it into a RecursiveIteratorIterator.

See some examples at

The default iteration mode is only to list leaves. If you also want the containing nodes to appear in the iteration, pass RecursiveIteratorIterator::SELF_FIRST as the second argument to the constructor of the RecursiveIteratorIterator

Well, as you can see here

public RecursiveIterator RecursiveIterator::getChildren ( void )

Returns an iterator for the current iterator entry. 

the method should return an object implementing the iterator. Your method return a simple array.

My guess would be to return something like:

public function getChildren(){
    return new Question($this->subquestions);
}

This is because you're using a RECURSIVE iterator so it's expected to have each node of the tree of the same type (an iterator)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!