website/errors/enum.constructor.md
<?php declare(strict_types = 1);
enum Suit
{
case Hearts;
case Diamonds;
public function __construct()
{
}
}
PHP enums cannot define a constructor. Enum cases are instantiated by PHP itself and are not meant to be created manually. Declaring a __construct method in an enum is a compile-time error.
Remove the constructor from the enum. If you need initialization logic, use a regular method instead:
<?php declare(strict_types = 1);
enum Suit
{
case Hearts;
case Diamonds;
- public function __construct()
- {
- }
+ public function label(): string
+ {
+ return match($this) {
+ self::Hearts => 'Hearts',
+ self::Diamonds => 'Diamonds',
+ };
+ }
}