Removes an object and its metadata from the storage.
The examples focus on the operation itself, including the important distinction: removeAll can remove several objects in one operation.
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::detach(object $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";
$store->detach($two);
echo json_encode([count($store), $store->contains($two)]);
Output from a successful local PHP 8.4 run:
[1,false]
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";
$store->detach($two);
echo json_encode([count($store), $store->contains($two)]);
Output from a successful local PHP 8.4 run:
[1,false]
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";
$store->detach($two);
echo json_encode([count($store), $store->contains($two)]);
Output from a successful local PHP 8.4 run:
[1,false]
Important Behavior and Edge Cases
A missing object is not removed; the same instance must be provided.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplObjectStorage::detach | Removes an object and its metadata from the storage. |
| Related API or alternative | removeAll can remove several objects in one operation. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | A missing object is not removed; the same instance must be provided. |
| Confusing it with a related API | removeAll can remove several objects in one operation. |
Key Takeaways
- Removes an object and its metadata from the storage.
- A missing object is not removed; the same instance must be provided.
- removeAll can remove several objects in one operation.
Use SplObjectStorage::detach when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.