Skip to content

Core\ObjectMap::remove

$objectMap->remove(K $key): void$objectMap->remove(
    K $key,
): void

Deletes a key and its value from a Core\ObjectMap. After remove, has returns false for that key, get returns null, and count is one lower. This replaces unset($storage[$object]) on an SplObjectStorage.

A key is found with ==. For an object of your own class, that is only the same object. Another object with equal property values is a different key, so remove does not delete it. A Core\Time value such as a Date is compared by its value, so an equal Date deletes the entry. If the key is not in the map, remove does nothing and does not throw an error.

After remove, the map no longer uses the key and the value. If nothing else uses them, they are freed.

The examples show removing one entry, removing a key that is not in the map, and a common use: tracking the jobs that are still running.

Parameters

  • $keyKThe key to delete. It matches a key that is == to it.

Return value

Returns void. Nothing. If the map does not have the key, the call does nothing and does not throw.

Examples

Remove an entry

<?nvs
// Shows how to delete one object and its value from a map.
final class Product
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Product $pen = new Product('pen');
Product $cup = new Product('cup');
Product $lamp = new Product('lamp');
Core\ObjectMap<Product, int> $stock = new Core\ObjectMap<Product, int>();
$stock->set($pen, 12);
$stock->set($cup, 4);
$stock->set($lamp, 1);
// `remove` deletes the cup and its value 4.
$stock->remove($cup);
// This prints 2, then "pen 12" and "lamp 1".
echo $stock->count(), "\n";
foreach ($stock as Product $product => int $amount) {
echo $product->name, ' ', $amount, "\n";
}
// The cup is not in the map any more. This prints "no".
echo $stock->has($cup) ? 'yes' : 'no', "\n";
Output
2
pen 12
lamp 1
no

A missing key does nothing

<?nvs
// Shows that removing a key that is not in the map does nothing.
final class Room
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Room $kitchen = new Room('kitchen');
Core\ObjectMap<Room, int> $lights = new Core\ObjectMap<Room, int>();
$lights->set($kitchen, 3);
// This object has the same name, but it is a different object.
// It is not a key in the map, so `remove` does nothing.
$lights->remove(new Room('kitchen'));
// This prints 1 and 3.
echo $lights->count(), "\n";
echo $lights->get($kitchen) ?? 0, "\n";
// You can remove the same key twice. The second call does nothing.
$lights->remove($kitchen);
$lights->remove($kitchen);
// This prints 0.
echo $lights->count(), "\n";
Output
1
3
0

Track running jobs

<?nvs
// Shows how to keep a list of the jobs that are still running, with the time each one started.
final class Job
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Job $backup = new Job('backup');
Job $report = new Job('report');
Job $email = new Job('email');
// The map stores the start time of each running job.
Core\ObjectMap<Job, string> $running = new Core\ObjectMap<Job, string>();
$running->set($backup, '09:00');
$running->set($report, '09:05');
$running->set($email, '09:07');
// When a job finishes, `remove` deletes it from the map.
array<Job> $finished = [$report, $backup];
foreach ($finished as Job $job) {
echo $job->name, " finished\n";
$running->remove($job);
}
// Only the email job is still running. This prints "email since 09:07".
foreach ($running as Job $job => string $started) {
echo $job->name, ' since ', $started, "\n";
}
Output
report finished
backup finished
email since 09:07