PHP.nl

Differences from objects

Differences from objects

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.

Magic methodsThe following object functionality is available, and behaves just as it does on any other object:

attributesTARGET_CLASS``TARGET_CLASS_CONST__call__callStatic__invoke__CLASS__``__FUNCTION__ The magic constant on an Enum type evaluates to the type name including any namespace, exactly the same as an object. The magic constant on a Case instance also evaluates to the Enum type, as it is an instance of that type. ::class``::class

Additionally, enum cases may not be instantiated directly with , nor with in reflection. Both will result in an error. new``ReflectionClass::newInstanceWithoutConstructor

<?php

$clovers = new Suit();
// Error: Cannot instantiate enum Suit

$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
// Error: Cannot instantiate enum Suit
?>