Although Enums are built on classes and objects, they do not support all object-related functionality. In particular, Enum cases are forbidden from having state.
The following object functionality is available, and behaves just as it does on any other object:
TARGET_CLASS
target
filter includes Enums themselves. The TARGET_CLASS_CONST
target filter
includes Enum Cases.
__CLASS__
and __FUNCTION__
constants behave as normal
The ::class
magic constant on an Enum type evaluates to the type
name including any namespace, exactly the same as an object. The ::class
magic constant on a Case instance also evaluates to the Enum type, as it is an
instance of that type.
Additionally, enum cases may not be instantiated directly with new
, nor with
ReflectionClass::newInstanceWithoutConstructor() in reflection. Both will result in an error.
<?php
$clovers = new Suit();
/ Error: Cannot instantiate enum Suit
$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
/ Error: Cannot instantiate enum Suit
?>
Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!
Check out: eBank.nz (Art Generator) |
Netwrck.com (AI Tools) |
Text-Generator.io (AI API) |
BitBank.nz (Crypto AI) |
ReadingTime (Kids Reading) |
RewordGame |
BigMultiplayerChess |
WebFiddle |
How.nz |
Helix AI Assistant