0
0
PHPprogramming~10 mins

Iterator interface implementation in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the iterator at the first element.

PHP
<?php
class MyIterator implements Iterator {
    private array $items = [1, 2, 3];
    private int $position = 0;

    public function rewind(): void {
        $this->position = [1];
    }

    public function current(): mixed {
        return $this->items[$this->position];
    }

    public function key(): int {
        return $this->position;
    }

    public function next(): void {
        $this->position++;
    }

    public function valid(): bool {
        return isset($this->items[$this->position]);
    }
}
?>
Drag options to blanks, or click blank then click option'
A-1
B1
C0
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Setting position to 1 skips the first element.
Using null or -1 causes errors when accessing the array.
2fill in blank
medium

Complete the code to check if the current position is valid.

PHP
<?php
public function valid(): bool {
    return [1]($this->items[$this->position]);
}
Drag options to blanks, or click blank then click option'
Aisset
Bis_null
Carray_key_exists
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty() returns false for zero or empty strings, which may be valid elements.
Using is_null() checks value, not existence.
Using array_key_exists() requires the key, but here we check the value.
3fill in blank
hard

Fix the error in the next() method to correctly advance the iterator.

PHP
<?php
public function next(): void {
    $this->position [1];
}
Drag options to blanks, or click blank then click option'
A--
B-=
C+=
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- moves backward, causing incorrect iteration.
Using += without a value is invalid syntax.
4fill in blank
hard

Fill both blanks to implement the current() method returning the current item.

PHP
<?php
public function current(): mixed {
    return $this->items[1];
}
Drag options to blanks, or click blank then click option'
A->position
B[$this->position]
C[position]
D($this->position)
Attempts:
3 left
💡 Hint
Common Mistakes
Using object property access ->position instead of array index.
Using parentheses instead of square brackets causes errors.
5fill in blank
hard

Fill all three blanks to complete the Iterator class with correct methods.

PHP
<?php
class MyIterator implements Iterator {
    private array $items = [10, 20, 30];
    private int $position = 0;

    public function rewind(): void {
        $this->position = [1];
    }

    public function current(): mixed {
        return $this->items[2];
    }

    public function valid(): bool {
        return [3]($this->items[$this->position]);
    }
}
?>
Drag options to blanks, or click blank then click option'
A0
B[$this->position]
Cisset
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting position to 1 skips the first element.
Using parentheses instead of square brackets for array access.
Using empty() or is_null() instead of isset().