0
0
PHPprogramming~5 mins

IteratorAggregate interface in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AgetIterator()
Bcurrent()
Cnext()
Drewind()
What should getIterator() return?
AAn object implementing Traversable
BAn array
CA string
DA boolean
Which interface requires implementing current(), next(), and rewind() methods?
ACountable
BIteratorAggregate
CArrayAccess
DIterator
Why use IteratorAggregate instead of Iterator?
ATo avoid using foreach loops
BTo separate iteration logic by returning an external iterator
CTo implement multiple iterators at once
DTo make the class non-iterable
Which of these is a valid iterator class to return from getIterator()?
AException
BDateTime
CArrayIterator
DPDO
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.