Core\ObjectSet::remove
Takes a value out of a Core\ObjectSet. After remove, has returns false for that value and the count is one less. remove returns nothing. This replaces $storage->detach($object) on an SplObjectStorage.
A value is found with ==. For an object of your own class, remove takes out only the same object that was added. An object with equal property values is a different object, and the set does not change. A Core\Time value such as a Date is compared by its value, so an equal Date is removed. If the value is not in the set, remove does nothing and does not throw an error.
After remove, the set no longer uses the value. If nothing else uses it, it is freed.
The examples show the set before and after remove, a value that is not in the set, and a common use: a list of users who are online, where a user is removed when they log out.
Parameters
$valueTThe value to remove, matched with==.
Return value
Returns void. Nothing. If the set does not contain the value, the set does not change.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\ObjectMap and Core\ObjectSet find a key with ==. An equal Core\Time value is the same key, so Date::at(2026, 3, 9) finds the entry of another Date for that day. Objects of your own classes are still found only by identity. |
Examples
Remove one value
<?nvs// Shows how `remove` takes one value out of a set.
final class Task{ public string $title;
public function constructor(string $title) { $this->title = $title; }}
Task $shop = new Task('Buy milk');Task $call = new Task('Call Bea');Task $mail = new Task('Post letter');
Core\ObjectSet<Task> $todo = new Core\ObjectSet<Task>();$todo->add($shop);$todo->add($call);$todo->add($mail);
$todo->remove($call);
// This prints 2.echo $todo->count(), "\n";// This prints "Buy milk" and then "Post letter".foreach ($todo as Task $task) { echo $task->title, "\n";}2Buy milkPost letterRemove a value that is not there
<?nvs// Shows that `remove` does nothing when the set does not have the value.
final class Card{ public string $number;
public function constructor(string $number) { $this->number = $number; }}
Card $saved = new Card('4111');Card $copy = new Card('4111');
Core\ObjectSet<Card> $wallet = new Core\ObjectSet<Card>();$wallet->add($saved);
// `$copy` has the same number, but it is a different object. The set does not change.$wallet->remove($copy);// This prints 1.echo $wallet->count(), "\n";
// Removing the same value twice is allowed. The second call does nothing.$wallet->remove($saved);$wallet->remove($saved);// This prints 0.echo $wallet->count(), "\n";10Users who are online
<?nvs// Shows how to keep a list of users who are online, and remove each one when they log out.
final class User{ public string $name;
public function constructor(string $name) { $this->name = $name; }}
final class Presence{ public Core\ObjectSet<User> $online;
public function constructor() { $this->online = new Core\ObjectSet<User>(); }
public function logIn(User $user): void { $this->online->add($user); }
public function logOut(User $user): void { $this->online->remove($user); }
public function names(): string { string $list = ''; foreach ($this->online as User $user) { $list = $list . $user->name . ' '; } return $list; }}
User $ana = new User('Ana');User $li = new User('Li');User $sam = new User('Sam');
Presence $chat = new Presence();$chat->logIn($ana);$chat->logIn($li);$chat->logIn($sam);// This prints "Ana Li Sam".echo $chat->names(), "\n";
$chat->logOut($li);// Li logs out a second time from another tab. Nothing changes.$chat->logOut($li);// This prints "Ana Sam".echo $chat->names(), "\n";Ana Li SamAna Sam