Recursive iterators expose nested data one level at a time, while RecursiveIteratorIterator can flatten a hierarchy for traversal.
What Is RecursiveIteratorIterator::endChildren in PHP?
A callback invoked when the traversal leaves a nested level.
RecursiveIteratorIterator::endChildren()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
class TrackingIterator extends RecursiveIteratorIterator {
public array $events = [];
public function beginChildren(): void { $this->events[] = "begin children"; }
public function endChildren(): void { $this->events[] = "end children"; }
public function beginIteration(): void { $this->events[] = "begin iteration"; }
public function endIteration(): void { $this->events[] = "end iteration"; }
public function nextElement(): void { $this->events[] = "next element"; }
}
$source = new RecursiveArrayIterator(["category" => ["a" => 1, "b" => 2], "status" => "ready"]);
$iterator = new TrackingIterator($source, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($iterator as $value) { /* Traverse to trigger callbacks. */ }
print_r($iterator->events);
Output:
Array
(
[0] => begin iteration
[1] => begin children
[2] => next element
[3] => next element
[4] => end children
[5] => next element
[6] => end iteration
)
Explanation: A callback invoked when the traversal leaves a nested level.
Example 2: Testing Another Case
<?php
class TrackingIterator extends RecursiveIteratorIterator {
public array $events = [];
public function beginChildren(): void { $this->events[] = "begin children"; }
public function endChildren(): void { $this->events[] = "end children"; }
public function beginIteration(): void { $this->events[] = "begin iteration"; }
public function endIteration(): void { $this->events[] = "end iteration"; }
public function nextElement(): void { $this->events[] = "next element"; }
}
$source = new RecursiveArrayIterator(["category" => ["a" => 1, "b" => 2], "state" => "active"]);
$iterator = new TrackingIterator($source, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($iterator as $value) { /* Traverse to trigger callbacks. */ }
print_r($iterator->events);
Output:
Array
(
[0] => begin iteration
[1] => begin children
[2] => next element
[3] => next element
[4] => end children
[5] => next element
[6] => end iteration
)
Explanation: This lifecycle callback fires when leaving a child level during traversal.
Practical Example: Applying the Operation
<?php
class TrackingIterator extends RecursiveIteratorIterator {
public array $events = [];
public function beginChildren(): void { $this->events[] = "begin children"; }
public function endChildren(): void { $this->events[] = "end children"; }
public function beginIteration(): void { $this->events[] = "begin iteration"; }
public function endIteration(): void { $this->events[] = "end iteration"; }
public function nextElement(): void { $this->events[] = "next element"; }
}
$source = new RecursiveArrayIterator(["category" => ["a" => 1, "b" => 2], "mode" => "pending"]);
$iterator = new TrackingIterator($source, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($iterator as $value) { /* Traverse to trigger callbacks. */ }
print_r($iterator->events);
Output:
Array
(
[0] => begin iteration
[1] => begin children
[2] => next element
[3] => next element
[4] => end children
[5] => next element
[6] => end iteration
)
Explanation: Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
Important Behavior and Edge Cases
- This lifecycle callback fires when leaving a child level during traversal.
- Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
RecursiveIteratorIterator::endChildren | A callback invoked when the traversal leaves a nested level. |
RecursiveIteratorIterator::getDepth() | RecursiveArrayIterator exposes nested arrays, while RecursiveIteratorIterator walks through the nested levels. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming RecursiveIteratorIterator::endChildren() works on every scalar entry without inspecting the current traversal depth. | This lifecycle callback fires when leaving a child level during traversal. |
| Choosing RecursiveIteratorIterator::getDepth() without checking whether it provides the same result. | RecursiveArrayIterator exposes nested arrays, while RecursiveIteratorIterator walks through the nested levels. |
Key Takeaways
- A callback invoked when the traversal leaves a nested level.
- This lifecycle callback fires when leaving a child level during traversal.
- Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
The examples demonstrate where RecursiveIteratorIterator::endChildren fits within nested iteration. Verify the relevant preconditions whenever you adapt the code to real data.