Sven Luijten: Using interfaces in third-party packages

2024-01-11 #php / svenluijten.com

A post on enums & interfaces. I didn't realize you could implement an interface on an enum!

This way you get the best of both worlds: the default implementations are neatly grouped in an enum, but others can extend using their own class implementing the interface.

enum ColorOption: string implements Color
{
case Red = 'red';
case Blue = 'blue';
case Green = 'green';
 
public function name(): string
{
return $this->value;
}
}