Content · Web Development · PHP

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

Checks whether an exact live object instance is a key in the map. The lookup uses identity; two equal-looking objects are different keys.

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

Checks whether an exact live object instance is a key in the map.

The examples focus on the operation itself, including the important distinction: offsetGet reads the associated value, if present.

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::offsetExists($object): bool

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.

Declared return type: bool. 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];
echo $metadata->offsetExists($user) ? "stored" : "absent";

Output from a successful local PHP 8.4 run:

stored

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];
echo $metadata->offsetExists($user) ? "stored" : "absent";

Output from a successful local PHP 8.4 run:

stored

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];
echo $metadata->offsetExists($user) ? "stored" : "absent";

Output from a successful local PHP 8.4 run:

stored

Important Behavior and Edge Cases


The lookup uses identity; two equal-looking objects are different keys.

Comparison with Related Approaches


ApproachDifference
WeakMap::offsetExistsChecks whether an exact live object instance is a key in the map.
Related API or alternativeoffsetGet reads the associated value, if present.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThe lookup uses identity; two equal-looking objects are different keys.
Confusing it with a related APIoffsetGet reads the associated value, if present.

Key Takeaways


  • Checks whether an exact live object instance is a key in the map.
  • The lookup uses identity; two equal-looking objects are different keys.
  • offsetGet reads the associated value, if present.

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