Positions the storage iterator at a zero-based numeric offset.
The examples focus on the operation itself, including the important distinction: rewind moves to the first entry, without an offset argument.
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::seek(int $offset): 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
- offset: required.
Declared return type: void. Read the behavior notes for important cases such as empty collections and destructive iteration.
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->seek(1);
echo $store->current()->label;
Output from a successful local PHP 8.4 run:
beta
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->seek(1);
echo $store->current()->label;
Output from a successful local PHP 8.4 run:
green
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->seek(1);
echo $store->current()->label;
Output from a successful local PHP 8.4 run:
second
Important Behavior and Edge Cases
Requires PHP 8.4 or later; an invalid position throws OutOfBoundsException.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplObjectStorage::seek | Positions the storage iterator at a zero-based numeric offset. |
| Related API or alternative | rewind moves to the first entry, without an offset argument. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Requires PHP 8.4 or later; an invalid position throws OutOfBoundsException. |
| Confusing it with a related API | rewind moves to the first entry, without an offset argument. |
Key Takeaways
- Positions the storage iterator at a zero-based numeric offset.
- Requires PHP 8.4 or later; an invalid position throws OutOfBoundsException.
- rewind moves to the first entry, without an offset argument.
Use SplObjectStorage::seek when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.