Tests whether an object is attached using ArrayAccess syntax.
The examples focus on the operation itself, including the important distinction: contains performs the same identity membership test by method name.
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
SplObjectStorage::offsetExists($object)
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.
Example 1: Basic Use
SplObjectStorage retains object identity and can associate independent information with each object; it is not keyed by the label property.
<?php
$one = (object) ["label" => 'alpha'];
$two = (object) ["label" => 'beta'];
$three = (object) ["label" => 'gamma'];
$store = new SplObjectStorage();
$store[$one] = "pending";
$store[$two] = "approved";
echo $store->offsetExists($one) ? "known" : "unknown";
Output from a successful local PHP 8.4 run:
known
Example 2: Checking Behavior
SplObjectStorage retains object identity and can associate independent information with each object; it is not keyed by the label property.
<?php
$one = (object) ["label" => 'red'];
$two = (object) ["label" => 'green'];
$three = (object) ["label" => 'blue'];
$store = new SplObjectStorage();
$store[$one] = "pending";
$store[$two] = "approved";
echo $store->offsetExists($one) ? "known" : "unknown";
Output from a successful local PHP 8.4 run:
known
Example 3: Practical Example
SplObjectStorage retains object identity and can associate independent information with each object; it is not keyed by the label property.
<?php
$one = (object) ["label" => 'first'];
$two = (object) ["label" => 'second'];
$three = (object) ["label" => 'third'];
$store = new SplObjectStorage();
$store[$one] = "pending";
$store[$two] = "approved";
echo $store->offsetExists($one) ? "known" : "unknown";
Output from a successful local PHP 8.4 run:
known
Important Behavior and Edge Cases
Keys must be objects, not strings or integer IDs.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplObjectStorage::offsetExists | Tests whether an object is attached using ArrayAccess syntax. |
| Related API or alternative | contains performs the same identity membership test by method name. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Keys must be objects, not strings or integer IDs. |
| Confusing it with a related API | contains performs the same identity membership test by method name. |
Key Takeaways
- Tests whether an object is attached using ArrayAccess syntax.
- Keys must be objects, not strings or integer IDs.
- contains performs the same identity membership test by method name.
Use SplObjectStorage::offsetExists when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.