Weakmap And Enums¶
<?php
enum WeekDay: string
{
case Monday = 'monday';
case Tuesday = 'tuesday';
case Wednesday = 'wednesday';
case Thursday = 'thursday';
case Friday = 'friday';
case Saturday = 'saturday';
case Sunday = 'sunday';
}
class Foo
{
public function hello()
{
return "Hello world" . PHP_EOL;
}
}
$array = [];
$array[WeekDay::Monday->value] = new Foo();
// etc
echo $array[WeekDay::Monday->value]->hello();
$map = new WeakMap();
$map[WeekDay::Monday] = new Foo();
// etc
echo $map[WeekDay::Monday]->hello();
Have been reminded by a fellow PHP dev that, instead of a simple array, one could also use WeakMap…
Nice thing with this is that it is a real dictionary… and that it accepts objects as keys… and this includes Enum::case.
See Also¶
PHP Features¶
Last updated: 14 July 2026