只是为了完成上面简洁示例中的 shape 函数
<?php
接口 Colorful
{
public function color(): string;
}
枚举 Suit 实现 Colorful
{
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',
};
}
public function shape(): string
{
return match($this) {
Suit::Hearts => '❤️',
Suit::Diamonds => '💎',
Suit::Clubs => '♣️',
Suit::Spades => ' ♠️'
};
}
}
echo Suit::Diamonds->shape();
echo PHP_EOL;
echo Suit::Clubs->shape();