Menu
Error Identifier: enum.constructor
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
enum Suit
{
case Hearts;
case Diamonds;
public function __construct()
{
}
}
Why is it reported? #
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.
How to fix it #
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',
+ };
+ }
}
How to ignore this error #
You can use the identifier enum.constructor to ignore this error using a comment:
// @phpstan-ignore enum.constructor
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: enum.constructor
Rules that report this error #
- PHPStan\Rules\Classes\EnumSanityRule [1]