website/errors/class.extendsEnum.md
<?php declare(strict_types = 1);
enum Status
{
case Active;
case Inactive;
}
class MyClass extends Status
{
}
A class cannot extend an enum. Enums in PHP are a special type and do not support inheritance. Attempting to extend an enum will result in a fatal error at runtime.
In the example above, MyClass tries to extend the Status enum, which is not allowed.
Use the enum directly instead of trying to extend it. If additional functionality is needed, use composition:
<?php declare(strict_types = 1);
-class MyClass extends Status
+class MyClass
{
+ public function __construct(
+ private Status $status,
+ ) {
+ }
}
Or implement an interface if shared behaviour is needed:
<?php declare(strict_types = 1);
interface HasLabel
{
public function label(): string;
}
enum Status: string implements HasLabel
{
case Active = 'active';
case Inactive = 'inactive';
public function label(): string
{
return $this->value;
}
}