Skip to content

Core\ObjectMap::get

$objectMap->get(K $key): ?V$objectMap->get(
    K $key,
): ?V

Returns the value stored for a key in a Core\ObjectMap. If the map has no such key, get returns null. It does not throw an error. This replaces reading an SplObjectStorage with $storage[$object].

A key is found with ==. For an object of your own class, == is true only for the same object. Another object with equal property values is a different key, so the result for it is null. A Core\Time value such as a Date is compared by its value. A Date that you build again for the same day finds the same entry.

The result type is ?V, so you can write $map->get($key) ?? $default to give a default. A map can also store null as a value. In that case get returns null too. Use has when you need to know if the key is in the map.

The examples show a lookup, a missing key, and a common use: storing a computed result for each object, so the program computes it only once. The last one finds a holiday with a Date read from text.

Parameters

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

Return value

Returns ?V. The value stored under $key, or null if the map has no such key. A stored null also returns null. Use has to tell the two cases apart.

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

Look up a value

<?nvs
// Shows how to read the value that is stored for a key with `get`.
final class Room
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Room $kitchen = new Room('Kitchen');
Room $hall = new Room('Hall');
Core\ObjectMap<Room, int> $temperature = new Core\ObjectMap<Room, int>();
$temperature->set($kitchen, 21);
$temperature->set($hall, 18);
// `get` returns the value for the key. This prints 21 and 18.
echo $kitchen->name, ': ', $temperature->get($kitchen) ?? 0, "\n";
echo $hall->name, ': ', $temperature->get($hall) ?? 0, "\n";
// After a new `set`, `get` returns the new value. This prints 23.
$temperature->set($kitchen, 23);
echo $kitchen->name, ': ', $temperature->get($kitchen) ?? 0, "\n";
Output
Kitchen: 21
Hall: 18
Kitchen: 23

A missing key returns null

<?nvs
// Shows what `get` returns for a key that is not in the map, and how `has` tells this case apart.
final class User
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
User $ada = new User('Ada');
User $bob = new User('Bob');
User $cy = new User('Cy');
Core\ObjectMap<User, ?string> $nickname = new Core\ObjectMap<User, ?string>();
$nickname->set($ada, 'Addy');
// Bob is in the map, but his value is `null`.
$nickname->set($bob, null);
// `get` returns `null` for Bob and for Cy. The `??` operator gives a default.
// This prints "Addy", "-" and "-".
echo $nickname->get($ada) ?? '-', "\n";
echo $nickname->get($bob) ?? '-', "\n";
echo $nickname->get($cy) ?? '-', "\n";
// `has` checks if the key is in the map. This prints `true` for Bob and `false` for Cy.
echo $nickname->has($bob) ? 'true' : 'false', "\n";
echo $nickname->has($cy) ? 'true' : 'false', "\n";
Output
Addy
-
-
true
false

Remember a result per object

<?nvs
// Shows how to store a computed result for each object, so it is computed only once.
final class Cart
{
public array<int> $prices;
public function constructor(array<int> $prices)
{
$this->prices = $prices;
}
}
final class Totals
{
public static function of(Core\ObjectMap<Cart, int> $known, Cart $cart): int
{
// `get` returns the stored total, or `null` when this cart has none yet.
?int $total = $known->get($cart);
if ($total != null) {
return $total;
}
int $sum = 0;
foreach ($cart->prices as int $price) {
$sum = $sum + $price;
}
echo "computed a total\n";
$known->set($cart, $sum);
return $sum;
}
}
Cart $cart = new Cart([1200, 350, 99]);
Core\ObjectMap<Cart, int> $known = new Core\ObjectMap<Cart, int>();
// The first call computes the total. The second call gets it from the map.
// This prints "computed a total" once, then 1649 twice.
echo Totals::of($known, $cart), "\n";
echo Totals::of($known, $cart), "\n";
Output
computed a total
1649
1649

A date built again finds its value

<?nvs
// Shows that a map keyed by `Core\Time\Date` finds a value with a date that was built again.
Core\ObjectMap<Core\Time\Date, string> $holidays = new Core\ObjectMap<Core\Time\Date, string>();
$holidays->set(Core\Time\Date::at(2026, 12, 25), 'Christmas Day');
$holidays->set(Core\Time\Date::at(2027, 1, 1), 'New Year');
// This date is a new object, read from text. A `Date` is compared by its day,
// so `get` finds the entry. This prints "Christmas Day".
var $asked = Core\Time\Date::fromIso('2026-12-25');
echo $holidays->get($asked) ?? 'a working day', "\n";
// No entry has this day. This prints "a working day".
echo $holidays->get(Core\Time\Date::at(2026, 12, 28)) ?? 'a working day', "\n";
Output
Christmas Day
a working day