Recall & Review
beginner
What is the purpose of the
IteratorAggregate interface in PHP?The <code>IteratorAggregate</code> interface allows a class to specify an external iterator by implementing the <code>getIterator()</code> method, which returns an instance of <code>Traversable</code>. This lets the class be used in <code>foreach</code> loops.Click to reveal answer
beginner
Which method must a class implement when it uses the <code>IteratorAggregate</code> interface?A class must implement the <code>getIterator()</code> method, which returns an object that implements <code>Traversable</code>, usually an <code>Iterator</code>.Click to reveal answer
intermediate
How does
IteratorAggregate differ from Iterator in PHP?IteratorAggregate requires only one method getIterator() that returns an iterator, while Iterator requires multiple methods (current(), next(), key(), valid(), rewind()) to control iteration directly.Click to reveal answer
beginner
What type of object should <code>getIterator()</code> return in a class implementing <code>IteratorAggregate</code>?The
getIterator() method should return an object that implements Traversable, typically an instance of Iterator or ArrayIterator.Click to reveal answer
intermediate
Why would you use
IteratorAggregate instead of Iterator?Using <code>IteratorAggregate</code> lets you separate the iteration logic from the class itself by returning an external iterator. This can simplify the class and reuse existing iterator classes.Click to reveal answer
Which method is required by the
IteratorAggregate interface?✗ Incorrect
The
IteratorAggregate interface requires the getIterator() method only.What should
getIterator() return?✗ Incorrect
getIterator() must return an object that implements Traversable.Which interface requires implementing
current(), next(), and rewind() methods?✗ Incorrect
Iterator requires these methods to control iteration.Why use
IteratorAggregate instead of Iterator?✗ Incorrect
IteratorAggregate allows returning an external iterator, separating iteration logic.Which of these is a valid iterator class to return from
getIterator()?✗ Incorrect
ArrayIterator implements Iterator and can be returned from getIterator().Explain how the
IteratorAggregate interface works and when you would use it.Think about how iteration is handled outside the class.
You got /5 concepts.
Describe the difference between
IteratorAggregate and Iterator interfaces in PHP.Focus on how iteration methods are implemented.
You got /4 concepts.