Menu

← Back to enum.*

Error Identifier: enum.magicMethod

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: string
{
	case Hearts = 'hearts';
	case Diamonds = 'diamonds';

	public function __get(string $name): mixed
	{
		return null;
	}
}

Why is it reported? #

PHP enums are not allowed to contain most magic methods. Only a limited set of magic methods is permitted in enums (__call, __callStatic, __invoke). Methods like __get, __set, __isset, __unset, __serialize, __unserialize, __toString, __clone, and others are forbidden because enums are not meant to be modified or have their behavior customized in ways that could break value semantics.

How to fix it #

Remove the disallowed magic method from the enum and use a regular named method instead:

 <?php declare(strict_types = 1);
 
 enum Suit: string
 {
 	case Hearts = 'hearts';
 	case Diamonds = 'diamonds';

-	public function __get(string $name): mixed
-	{
-		return null;
-	}
+	public function getLabel(): string
+	{
+		return match($this) {
+			self::Hearts => 'Hearts',
+			self::Diamonds => 'Diamonds',
+		};
+	}
 }

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Classes\EnumSanityRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.