Content · Web Development · PHP

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

Adds or updates metadata for an object key without retaining it strongly. The key must be an object; dropping all other references removes its entry.

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

Adds or updates metadata for an object key without retaining it strongly.

The examples focus on the operation itself, including the important distinction: SplObjectStorage, unlike WeakMap, retains strong object references.

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::offsetSet($object, mixed $value): void

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.

Parameters


  • object: required.
  • value: required.

Declared return type: void. 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];
$metadata->offsetSet($other, ["score" => 9]);
echo count($metadata);

Output from a successful local PHP 8.4 run:

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];
$metadata->offsetSet($other, ["score" => 9]);
echo count($metadata);

Output from a successful local PHP 8.4 run:

2

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];
$metadata->offsetSet($other, ["score" => 9]);
echo count($metadata);

Output from a successful local PHP 8.4 run:

2

Important Behavior and Edge Cases


The key must be an object; dropping all other references removes its entry.

Comparison with Related Approaches


ApproachDifference
WeakMap::offsetSetAdds or updates metadata for an object key without retaining it strongly.
Related API or alternativeSplObjectStorage, unlike WeakMap, retains strong object references.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThe key must be an object; dropping all other references removes its entry.
Confusing it with a related APISplObjectStorage, unlike WeakMap, retains strong object references.

Key Takeaways


  • Adds or updates metadata for an object key without retaining it strongly.
  • The key must be an object; dropping all other references removes its entry.
  • SplObjectStorage, unlike WeakMap, retains strong object references.

Use WeakMap::offsetSet 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.