array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'language.enumerations.object-differences.php', 1 => 'Differences from objects', ), 'up' => array ( 0 => 'language.enumerations.php', 1 => 'Enumerations', ), 'prev' => array ( 0 => 'language.enumerations.expressions.php', 1 => 'Enum values in constant expressions', ), 'next' => array ( 0 => 'language.enumerations.listing.php', 1 => 'Value listing', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'language/enumerations.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
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
?>