Returns an iterator over live object keys and their metadata values.
The examples focus on the operation itself, including the important distinction: offsetGet retrieves one object's metadata without traversing the map.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
WeakMap::getIterator(): Iterator
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Declared return type: Iterator. Read the behavior notes for important cases such as empty collections and destructive iteration.
Example 1: Basic Use
The map tracks an object only while another strong reference keeps it alive; keys use identity rather than a name string.
<?php
$user = (object) ["name" => 'Ari'];
$other = (object) ["name" => "guest"];
$metadata = new WeakMap();
$metadata[$user] = ["score" => 2];
foreach ($metadata->getIterator() as $object => $info) { echo $object->name . ":" . $info["score"]; }
Output from a successful local PHP 8.4 run:
Ari:2
Example 2: Checking Behavior
The map tracks an object only while another strong reference keeps it alive; keys use identity rather than a name string.
<?php
$user = (object) ["name" => 'Bo'];
$other = (object) ["name" => "guest"];
$metadata = new WeakMap();
$metadata[$user] = ["score" => 3];
foreach ($metadata->getIterator() as $object => $info) { echo $object->name . ":" . $info["score"]; }
Output from a successful local PHP 8.4 run:
Bo:3
Example 3: Practical Example
The map tracks an object only while another strong reference keeps it alive; keys use identity rather than a name string.
<?php
$user = (object) ["name" => 'Cam'];
$other = (object) ["name" => "guest"];
$metadata = new WeakMap();
$metadata[$user] = ["score" => 4];
foreach ($metadata->getIterator() as $object => $info) { echo $object->name . ":" . $info["score"]; }
Output from a successful local PHP 8.4 run:
Cam:4
Important Behavior and Edge Cases
The map never keeps an object alive solely because it is a key.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| WeakMap::getIterator | Returns an iterator over live object keys and their metadata values. |
| Related API or alternative | offsetGet retrieves one object's metadata without traversing the map. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | The map never keeps an object alive solely because it is a key. |
| Confusing it with a related API | offsetGet retrieves one object's metadata without traversing the map. |
Key Takeaways
- Returns an iterator over live object keys and their metadata values.
- The map never keeps an object alive solely because it is a key.
- offsetGet retrieves one object's metadata without traversing the map.
Use WeakMap::getIterator when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.