website/errors/classImplements.enum.md
<?php declare(strict_types = 1);
enum Suit
{
case Hearts;
case Diamonds;
}
class Card implements Suit // error
{
}
In PHP, a class can only implement interfaces. Enums are not interfaces and cannot be used in an implements clause. This is a fundamental language constraint -- the implements keyword is reserved exclusively for interface types.
Extract an interface that the enum implements, then have your class implement that interface instead:
<?php declare(strict_types = 1);
+interface HasLabel
+{
+ public function label(): string;
+}
+
-enum Suit
+enum Suit implements HasLabel
{
case Hearts;
case Diamonds;
+
+ public function label(): string
+ {
+ return $this->name;
+ }
}
-class Card implements Suit
+class Card implements HasLabel
{
+ public function label(): string
+ {
+ return 'Joker';
+ }
}