information overload

by sebastian de deyne

Sven Luijten: Using interfaces in third-party packages

11 Jan 2024 via 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;
    }
}