An iterator has a current position and advances only when traversal is requested.
What Is IteratorIterator::getInnerIterator in PHP?
Returns the wrapped iterator used as the source of values.
IteratorIterator::getInnerIterator()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
$iterator = new IteratorIterator(new ArrayIterator(["north"=>10,"south"=>20,"west"=>30]));
$iterator->rewind();
var_dump(get_class($iterator->getInnerIterator()));
Output:
string(13) "ArrayIterator"
Explanation: Returns the wrapped iterator used as the source of values.
Example 2: Testing Another Case
<?php
$iterator = new IteratorIterator(new ArrayIterator(["apple","pear","plum"]));
$iterator->rewind();
var_dump(get_class($iterator->getInnerIterator()));
Output:
string(13) "ArrayIterator"
Explanation: The returned value is the wrapped iterator, not the value it currently yields.
Practical Example: Applying the Operation
<?php
$iterator = new IteratorIterator(new ArrayIterator(["red"=>"ready","blue"=>"waiting","green"=>"done"]));
$iterator->rewind();
var_dump(get_class($iterator->getInnerIterator()));
Output:
string(13) "ArrayIterator"
Explanation: Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.
Important Behavior and Edge Cases
- The returned value is the wrapped iterator, not the value it currently yields.
- Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
IteratorIterator::getInnerIterator | Returns the wrapped iterator used as the source of values. |
IteratorIterator::current() | getInnerIterator() returns the wrapped iterator object; current() returns the current value produced by the wrapper. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the current iterator position when calling IteratorIterator::getInnerIterator(). | The returned value is the wrapped iterator, not the value it currently yields. |
| Choosing IteratorIterator::current() without checking whether it provides the same result. | getInnerIterator() returns the wrapped iterator object; current() returns the current value produced by the wrapper. |
Key Takeaways
- Returns the wrapped iterator used as the source of values.
- The returned value is the wrapped iterator, not the value it currently yields.
- Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.
The examples demonstrate where IteratorIterator::getInnerIterator fits within traversable collections. Verify the relevant preconditions whenever you adapt the code to real data.