Copies object entries and their attached information from another SplObjectStorage.
The examples focus on the operation itself, including the important distinction: attach adds one object; addAll transfers all entries from another storage.
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::addAll(SplObjectStorage $storage)
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
- storage: 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";
$other = new SplObjectStorage();
$other[$two] = "from-other";
$other[$three] = "new";
$store->addAll($other);
echo json_encode([count($store), $store[$three]]);
Output from a successful local PHP 8.4 run:
[3,"new"]
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";
$other = new SplObjectStorage();
$other[$two] = "from-other";
$other[$three] = "new";
$store->addAll($other);
echo json_encode([count($store), $store[$three]]);
Output from a successful local PHP 8.4 run:
[3,"new"]
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";
$other = new SplObjectStorage();
$other[$two] = "from-other";
$other[$three] = "new";
$store->addAll($other);
echo json_encode([count($store), $store[$three]]);
Output from a successful local PHP 8.4 run:
[3,"new"]
Important Behavior and Edge Cases
Objects are keyed by identity; adding the same instance updates information rather than duplicating it.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplObjectStorage::addAll | Copies object entries and their attached information from another SplObjectStorage. |
| Related API or alternative | attach adds one object; addAll transfers all entries from another storage. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Objects are keyed by identity; adding the same instance updates information rather than duplicating it. |
| Confusing it with a related API | attach adds one object; addAll transfers all entries from another storage. |
Key Takeaways
- Copies object entries and their attached information from another SplObjectStorage.
- Objects are keyed by identity; adding the same instance updates information rather than duplicating it.
- attach adds one object; addAll transfers all entries from another storage.
Use SplObjectStorage::addAll when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.