Enums May Now Define __debugInfo()
Declaring the magic method __debugInfo() on an enum used to be a compile-time fatal error, because enum cases were treated like other magic methods that make no sense on enums. In PHP 8.6, __debugInfo() is allowed on enums, so var_dump() can render a custom representation of an enum case instead of the default one.
PHP code
<?php
enum Suit {
case Hearts;
case Spades;
public function __debugInfo(): array {
return ['custom' => $this->name];
}
}
var_dump(Suit::Hearts);
?>
Before
PHP Fatal error: Enum Suit cannot include magic method __debugInfo in /codes/enumDebugInfo86.php on line 3
Stack trace:
#0 {main}
Fatal error: Enum Suit cannot include magic method __debugInfo in /codes/enumDebugInfo86.php on line 3
Stack trace:
#0 {main}
After
enum(Suit::Hearts) (1) {
["custom"]=>
string(6) "Hearts"
}
PHP version change
This behavior changed in 8.6.