Skip to content

Core\ObjectMap::set

$objectMap->set(K $key, V $value): void$objectMap->set(
    K $key,
    V $value,
): void

Stores a value for a key in a Core\ObjectMap. If the key is new, set adds it at the end of the map. If the key is already in the map, set replaces its value and the key keeps its place. This replaces $storage[$object] = $value on an SplObjectStorage.

A key is found with ==. For an object of your own class, == is true only for the same object. Two objects with equal property values are two different keys, so each one gets its own entry. A Core\Time value such as a Date is compared by its value, so set with an equal Date replaces the value. A DateTime is compared by its moment. The same moment in two time zones is one key.

The value can be null, and the key is still in the map.

A key and its value are not freed while they are in the map. After remove or clear, they are freed if nothing else uses them.

The examples show adding and replacing a value, two equal objects as two keys, and a common use: counting how many times each object appears.

Parameters

  • $keyKThe key. It matches a key in the map that is == to it.
  • $valueVThe value to store under $key.

Return value

Returns void. Nothing. A key that is set again keeps its first place in the order of the keys.

Changelog

VersionChange
0.0.5Core\ObjectMap and Core\ObjectSet are faster. A lookup with a string, int or object key costs about the same as a lookup in an array. A foreach over either does not copy it.

Examples

Add and replace a value

<?nvs
// Shows how to store a value for an object, and how to replace it later.
final class Order
{
public string $number;
public function constructor(string $number)
{
$this->number = $number;
}
}
Order $first = new Order('A-1');
Order $second = new Order('A-2');
Core\ObjectMap<Order, string> $status = new Core\ObjectMap<Order, string>();
$status->set($first, 'new');
$status->set($second, 'new');
// The key is already in the map, so `set` replaces its value.
$status->set($first, 'shipped');
// The map still has two entries, and the first order keeps its place.
// This prints "A-1 shipped" and "A-2 new".
foreach ($status as Order $order => string $state) {
echo $order->number, ' ', $state, "\n";
}
Output
A-1 shipped
A-2 new

Equal objects are two keys

<?nvs
// Shows that two objects with equal property values are two different keys.
final class Point
{
public int $x;
public int $y;
public function constructor(int $x, int $y)
{
$this->x = $x;
$this->y = $y;
}
}
Point $a = new Point(1, 2);
Point $b = new Point(1, 2);
Core\ObjectMap<Point, string> $labels = new Core\ObjectMap<Point, string>();
$labels->set($a, 'start');
$labels->set($b, 'end');
// `$a` and `$b` have the same property values, but they are two objects.
// `$a == $b` is false, so this prints 2.
echo $labels->count(), "\n";
// Each object has its own value. This prints "start", then "end".
echo $labels->get($a) ?? '-', "\n";
echo $labels->get($b) ?? '-', "\n";
Output
2
start
end

Count visits per user

<?nvs
// Shows how to count how many times each object appears in a list.
final class User
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
User $ana = new User('Ana');
User $li = new User('Li');
array<User> $visits = [$ana, $li, $ana, $ana, $li];
Core\ObjectMap<User, int> $count = new Core\ObjectMap<User, int>();
foreach ($visits as User $user) {
// `get` returns `null` the first time, so `?? 0` starts the count at 0.
$count->set($user, ($count->get($user) ?? 0) + 1);
}
// This prints "Ana 3" and "Li 2".
foreach ($count as User $user => int $visitCount) {
echo $user->name, ' ', $visitCount, "\n";
}
Output
Ana 3
Li 2