Skip to content

Core\ObjectMap

A map whose keys are objects or other values of any type. A key is found with ==. For most objects, only the same object matches. A Core\Time value, such as a Date, matches an equal value. Build one with new Core\ObjectMap<K, V>(). A foreach over the map gives its keys in insertion order.

Core\ObjectMap<K, V> associates a value with an object, and finds a key with ==. For most objects that is identity: the one object is one key however many variables hold it, and two objects that agree on every property are two keys. A Core\Time value, such as a Date or a Duration, compares by value, so an equal date built elsewhere finds the same entry. array<T> cannot key on an object, so this is where a per-object side table lives. It is built with new Core\ObjectMap<K, V>(), get answers ?V rather than throwing — there is no $m[$k] subscript on it — and has is the one member that tells an absent key from a stored null.

A foreach loop walks the map in the order the keys were added. With a key variable, foreach ($map as var $key => var $value) gives each key with its value. With only a value variable, the loop gives the keys. A foreach does not copy the map. If the body changes the map, the loop still walks the entries the map had when it began. So the body may remove the entry it is on.

An object whose class implements Revocable leaves the map when the program calls revoke() on it. The map removes every entry that has the object as its key or as its value. count() is then correct, and a running foreach skips the removed entries.

<?nvs
class Tag
{
public function constructor(public string $name)
{}
}
var $red = new Tag('red');
var $blue = new Tag('blue');
var $weights = new Core\ObjectMap<Tag, int>();
$weights->set($red, 3);
$weights->set($blue, 7);
$weights->set($red, 4);
echo 'red=', $weights->get($red) ?? 0, "\n";
echo 'other red=', $weights->get(new Tag('red')) ?? -1, "\n";
echo 'count=', $weights->count(), "\n";
foreach ($weights as Tag $tag => int $weight) {
echo $tag->name, '=', $weight, ' ';
}
echo "\n";
$weights->remove($blue);
echo $weights->has($blue) ? 'blue kept' : 'blue gone', "\n";
echo 'keys=', Core\Arr::count($weights->keys()), ' values=', Core\Arr::count($weights->values()), "\n";
Output
red=4
other red=-1
count=2
red=4 blue=7
blue gone
keys=1 values=1

Members

9 members.

MemberSignature
setset(K $key, V $value): void
getget(K $key): ?V
hashas(K $key): bool
removeremove(K $key): void
countcount(): uint
isEmptyisEmpty(): bool
keyskeys(): array<K>
valuesvalues(): array<V>
clearclear(): void