Challenge - 5 Problems
IteratorAggregate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of IteratorAggregate with simple array
What is the output of this PHP code using IteratorAggregate?
PHP
<?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 . ' '; }
Attempts:
2 left
💡 Hint
Remember that IteratorAggregate requires getIterator() to return an iterator object.
✗ Incorrect
The getIterator() method returns an ArrayIterator over the items array. The foreach loop iterates over these items and prints them separated by spaces.
❓ Predict Output
intermediate2:00remaining
Output when getIterator returns a generator
What will this PHP code output when using IteratorAggregate with a generator?
PHP
<?php class GenCollection implements IteratorAggregate { public function getIterator(): Traversable { yield 10; yield 20; yield 30; } } $gen = new GenCollection(); foreach ($gen as $value) { echo $value . '-'; }
Attempts:
2 left
💡 Hint
Generators implement Traversable and can be returned by getIterator.
✗ Incorrect
The getIterator method yields values 10, 20, and 30. The foreach prints each value followed by a dash.
🔧 Debug
advanced2:00remaining
Identify the error in IteratorAggregate implementation
What error will this PHP code produce?
PHP
<?php class BadCollection implements IteratorAggregate { private array $data = [5, 6, 7]; public function getIterator(): array { return $this->data; } } $bad = new BadCollection(); foreach ($bad as $item) { echo $item; }
Attempts:
2 left
💡 Hint
Check the return type of getIterator method.
✗ Incorrect
The getIterator method must return an object implementing Traversable, not an array. Returning an array causes a fatal error.
📝 Syntax
advanced2:00remaining
Which option correctly implements IteratorAggregate?
Which of the following PHP code snippets correctly implements IteratorAggregate?
Attempts:
2 left
💡 Hint
getIterator must return Traversable, not array.
✗ Incorrect
Only option B returns an object implementing Traversable (ArrayIterator). Options A, B, and D return arrays or lack return type, causing errors.
🚀 Application
expert3:00remaining
Count items using IteratorAggregate and Countable
Given this PHP class implementing IteratorAggregate and Countable, what is the output of the code?
PHP
<?php class CountableCollection implements IteratorAggregate, Countable { private array $elements = ['a', 'b', 'c', 'd']; public function getIterator(): Traversable { return new ArrayIterator($this->elements); } public function count(): int { return count($this->elements); } } $col = new CountableCollection(); echo count($col) . ' '; foreach ($col as $item) { echo $item; }
Attempts:
2 left
💡 Hint
Countable interface allows count() to work on the object.
✗ Incorrect
The count() method returns 4, the number of elements. The foreach prints each element in order.