Content · Web Development · PHP

PHP WeakMap::getIterator() Method: Usage and Examples

Returns an iterator over live object keys and their metadata values. The map never keeps an object alive solely because it is a key.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

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


ApproachDifference
WeakMap::getIteratorReturns an iterator over live object keys and their metadata values.
Related API or alternativeoffsetGet retrieves one object's metadata without traversing the map.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThe map never keeps an object alive solely because it is a key.
Confusing it with a related APIoffsetGet 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.

Share this resourceSend this page to someone who may find it useful.