Skip to content

Core\ObjectMap::clear

$objectMap->clear(): void

Removes every entry from a Core\ObjectMap. After clear, count returns 0 and isEmpty returns true.

clear does not delete the objects you used as keys or values. Other variables that refer to them still work. Only the map forgets them. If nothing else refers to an object, its memory is freed.

The map itself stays usable. You can call set on it again right away, and a second clear on an empty map does nothing.

A common use is a cache for one batch of work. The program fills the map while it handles the batch, and calls clear before the next batch starts. The examples show this, and they show that the keys still exist after clear.

Return value

Returns void. Nothing. You can add new keys to the same map afterwards.

Examples

Empty a map

<?nvs
// Shows how to remove every entry from a map with `clear`.
final class Order
{
public string $number;
public function constructor(string $number)
{
$this->number = $number;
}
}
Core\ObjectMap<Order, string> $status = new Core\ObjectMap<Order, string>();
$status->set(new Order('A-100'), 'paid');
$status->set(new Order('A-101'), 'shipped');
$status->set(new Order('A-102'), 'open');
// This prints 3.
echo $status->count(), "\n";
// `clear` removes all three entries. The map is now empty, so this prints 0 and `true`.
$status->clear();
echo $status->count(), "\n";
echo $status->isEmpty() ? 'true' : 'false', "\n";
Output
3
0
true

The keys still exist

<?nvs
// Shows that `clear` removes the entries from the map, but not the objects you used as keys.
final class Player
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Player $ana = new Player('Ana');
Player $ben = new Player('Ben');
Core\ObjectMap<Player, int> $score = new Core\ObjectMap<Player, int>();
$score->set($ana, 12);
$score->set($ben, 7);
$score->clear();
// The variables `$ana` and `$ben` still work. The map has no score for them,
// so `get` returns `null` and this prints "Ana: none".
echo $ana->name, ': ', $score->get($ana) ?? 'none', "\n";
// You can fill the same map again after `clear`. This prints "Ben: 3".
$score->set($ben, 3);
echo $ben->name, ': ', $score->get($ben) ?? 'none', "\n";
Output
Ana: none
Ben: 3

A cache for one batch

<?nvs
// Shows a cache that is emptied after each batch of work, so old results are never used again.
final class Customer
{
public string $name;
public int $orders;
public function constructor(string $name, int $orders)
{
$this->name = $name;
$this->orders = $orders;
}
}
final class Discounts
{
// The cache has one computed discount per customer object.
public static function of(Core\ObjectMap<Customer, int> $cache, Customer $c): int
{
?int $known = $cache->get($c);
if ($known != null) {
return $known;
}
int $percent = $c->orders >= 10 ? 15 : 5;
$cache->set($c, $percent);
return $percent;
}
}
Customer $mia = new Customer('Mia', 12);
Customer $tom = new Customer('Tom', 2);
array<array<Customer>> $batches = [[$mia, $tom, $mia], [$tom, $tom]];
Core\ObjectMap<Customer, int> $cache = new Core\ObjectMap<Customer, int>();
foreach ($batches as array<Customer> $batch) {
foreach ($batch as Customer $c) {
echo $c->name, ' gets ', Discounts::of($cache, $c), "%\n";
}
// This prints how many discounts the batch computed: 2, then 1.
// Then `clear` empties the cache for the next batch.
echo 'cached: ', $cache->count(), "\n";
$cache->clear();
}
Output
Mia gets 15%
Tom gets 5%
Mia gets 15%
cached: 2
Tom gets 5%
Tom gets 5%
cached: 1