PHP.nl

Traits

Traits

Enumerations may leverage traits, which will behave the same as on classes. The caveat is that traits d in an enum must not contain properties. They may only include methods, static methods, and constants. A trait with properties will result in a fatal error. use

<?php

interface Colorful
{
    public function color(): string;
}

trait Rectangle
{
    public function shape(): string {
        return "Rectangle";
    }
}

enum Suit implements Colorful
{
    use Rectangle;

    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;

    public function color(): string
    {
        return match($this) {
            Suit::Hearts, Suit::Diamonds => 'Red',
            Suit::Clubs, Suit::Spades => 'Black',
        };
    }
}
?>