Dynamic Enum Cases¶
<?php
enum E: string {
case A = 'abc';
const B = 'def';
}
$name = 'E::A';
echo constant($name)->value;
$name = 'E::B';
echo constant($name);
PHP offers the constant() function to reach the value of a constant, when knowing its name.
This also works for class constant, using the class::constant name. This is an alternative to the class::{$name} syntax, that was introduced in PHP 8.3.
And the constant() call also works on enumeration cases, which are, eventually, also class constants.
See Also¶
Dynamic Enum Cases [Try me]
PHP Features¶
Last updated: 14 July 2026