0
0
PHPprogramming~20 mins

IteratorAggregate interface in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IteratorAggregate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 . ' ';
}
A1 2 3
BArrayIterator Object
CError: getIterator must return an array
D1,2,3
Attempts:
2 left
💡 Hint
Remember that IteratorAggregate requires getIterator() to return an iterator object.
Predict Output
intermediate
2: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 . '-';
}
A10-20-30-
BArrayIterator Object
CFatal error: getIterator must return an Iterator object
D10 20 30
Attempts:
2 left
💡 Hint
Generators implement Traversable and can be returned by getIterator.
🔧 Debug
advanced
2: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;
}
ASyntax error: missing semicolon
BOutputs: 567
CFatal error: getIterator must return an instance of Traversable
DTypeError: Cannot convert array to string
Attempts:
2 left
💡 Hint
Check the return type of getIterator method.
📝 Syntax
advanced
2:00remaining
Which option correctly implements IteratorAggregate?
Which of the following PHP code snippets correctly implements IteratorAggregate?
Aclass C implements IteratorAggregate { public function getIterator(): array { return [1,2]; } }
Bclass C implements IteratorAggregate { public function getIterator(): Traversable { return new ArrayIterator([1,2]); } }
Cclass C implements IteratorAggregate { public function getIterator(): Traversable { return [1,2]; } }
Dclass C implements IteratorAggregate { public function getIterator() { return [1,2]; } }
Attempts:
2 left
💡 Hint
getIterator must return Traversable, not array.
🚀 Application
expert
3: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;
}
Aabcd 4
BError: Countable not implemented correctly
C4 ArrayIterator Object
D4 abcd
Attempts:
2 left
💡 Hint
Countable interface allows count() to work on the object.