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()
✗ Incorrect
The IteratorAggregate interface requires the getIterator() method only.
What should getIterator() return?
AAn object implementing Traversable
BAn array
CA string
DA boolean
✗ Incorrect
getIterator() must return an object that implements Traversable.
Which interface requires implementing current(), next(), and rewind() methods?
ACountable
BIteratorAggregate
CArrayAccess
DIterator
✗ Incorrect
Iterator requires these methods to control iteration.
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
✗ Incorrect
IteratorAggregate allows returning an external iterator, separating iteration logic.
Which of these is a valid iterator class to return from getIterator()?
AException
BDateTime
CArrayIterator
DPDO
✗ 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.
Practice
(1/5)
1.
What is the main purpose of the IteratorAggregate interface in PHP?
easy
A. To allow an object to be iterated with foreach by providing an external iterator.
B. To automatically sort arrays inside an object.
C. To enforce type checking on array elements.
D. To convert an object into a string representation.
Solution
Step 1: Understand the role of IteratorAggregate
The interface requires implementing getIterator() which returns an iterator object.
Step 2: Connect to foreach usage
This allows the object to be used in foreach loops by providing the iterator externally.
Final Answer:
To allow an object to be iterated with foreach by providing an external iterator. -> Option A
Quick Check:
IteratorAggregate enables foreach iteration [OK]
Hint: Remember: IteratorAggregate provides an iterator for foreach [OK]
Common Mistakes:
Thinking it sorts data automatically
Confusing it with string conversion
Assuming it enforces type checks
2.
Which method must a class implement when it uses the IteratorAggregate interface?
easy
A. getItems()
B. iterate()
C. getIterator()
D. toArray()
Solution
Step 1: Recall interface requirements
The IteratorAggregate interface requires the getIterator() method.
Step 2: Confirm method name
This method must return an iterator object to enable iteration.
Final Answer:
getIterator() -> Option C
Quick Check:
IteratorAggregate requires getIterator() [OK]
Hint: Interface method is always getIterator() [OK]
Common Mistakes:
Using iterate() instead of getIterator()
Confusing with toArray() method
Assuming getItems() is required
3.
What will be the output of this PHP code?
<?php
class MyCollection implements IteratorAggregate {
private array $items = [1, 2, 3];
public function getIterator(): Traversable {
return new ArrayIterator($this->items);
}
}
$collection = new MyCollection();
foreach ($collection as $item) {
echo $item . ' ';
}
medium
A. ArrayIterator Object
B. 1 2 3
C. Error: getIterator must return array
D. No output
Solution
Step 1: Understand getIterator() return
The method returns an ArrayIterator over the array [1, 2, 3].
Step 2: foreach loops over iterator
The foreach loop iterates over the items 1, 2, and 3, echoing each with a space.
Final Answer:
1 2 3 -> Option B
Quick Check:
foreach prints each item from ArrayIterator [OK]
Hint: getIterator returns ArrayIterator to enable foreach [OK]
Common Mistakes:
Expecting an error because getIterator returns ArrayIterator
Thinking it prints object instead of values
Missing the space after each number
4.
Identify the error in this PHP class implementing IteratorAggregate:
<?php
class Numbers implements IteratorAggregate {
private array $nums = [10, 20, 30];
public function getIterator() {
return $this->nums;
}
}
medium
A. getIterator() must return an object implementing Traversable, not an array.
B. Property $nums should be public, not private.
C. Class must implement Iterator, not IteratorAggregate.
D. getIterator() method must be static.
Solution
Step 1: Check getIterator() return type
It returns an array, but getIterator() must return an object implementing Traversable.
Step 2: Understand interface contract
Returning an array causes a type error because arrays do not implement Traversable.
Final Answer:
getIterator() must return an object implementing Traversable, not an array. -> Option A
Quick Check:
getIterator() returns Traversable object [OK]
Hint: getIterator must return Traversable object, not array [OK]
Common Mistakes:
Returning array directly from getIterator()
Thinking private properties cause error here
Assuming getIterator() must be static
5.
You want to create a class FilteredCollection that implements IteratorAggregate and only iterates over items greater than 5 from an internal array. Which implementation of getIterator() is correct?
<?php
class FilteredCollection implements IteratorAggregate {
private array $items;
public function __construct(array $items) {
$this->items = $items;
}
public function getIterator(): Traversable {
// Which code below is correct?
}
}
hard
A. return new ArrayIterator($this->items);
B. return new ArrayIterator(array_map(fn($v) => $v > 5, $this->items));
C. return $this->items;
D. return new ArrayIterator(array_filter($this->items, fn($v) => $v > 5));
Solution
Step 1: Filter items greater than 5
Use array_filter with a callback to keep only values > 5.
Step 2: Return an ArrayIterator over filtered items
Wrap filtered array in ArrayIterator to satisfy Traversable return type.
Final Answer:
return new ArrayIterator(array_filter($this->items, fn($v) => $v > 5)); -> Option D
Quick Check:
Filter then wrap in ArrayIterator [OK]
Hint: Filter array first, then return ArrayIterator [OK]
Common Mistakes:
Returning unfiltered array directly
Using array_map instead of array_filter
Not wrapping result in an iterator object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.