Content · Web Development · PHP

PHP SplObjectStorage::removeAllExcept() Method: Usage and Examples

Retains only objects that also appear in the supplied storage. This is a filter by object identity, not a filter on attached metadata.

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

Retains only objects that also appear in the supplied storage.

The examples focus on the operation itself, including the important distinction: removeAll removes the matching entries instead.

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::removeAllExcept(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->removeAllExcept($other);
echo json_encode([count($store), $store->contains($two)]);

Output from a successful local PHP 8.4 run:

[1,true]

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->removeAllExcept($other);
echo json_encode([count($store), $store->contains($two)]);

Output from a successful local PHP 8.4 run:

[1,true]

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->removeAllExcept($other);
echo json_encode([count($store), $store->contains($two)]);

Output from a successful local PHP 8.4 run:

[1,true]

Important Behavior and Edge Cases


This is a filter by object identity, not a filter on attached metadata.

Comparison with Related Approaches


ApproachDifference
SplObjectStorage::removeAllExceptRetains only objects that also appear in the supplied storage.
Related API or alternativeremoveAll removes the matching entries instead.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThis is a filter by object identity, not a filter on attached metadata.
Confusing it with a related APIremoveAll removes the matching entries instead.

Key Takeaways


  • Retains only objects that also appear in the supplied storage.
  • This is a filter by object identity, not a filter on attached metadata.
  • removeAll removes the matching entries instead.

Use SplObjectStorage::removeAllExcept 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.