2

Is it possible to access enumerations properties dynamically?

Here is what I tried and the error I got.

Access to undeclared static property ExampleEnum::$id

enum ExampleEnum
{
    case _1;
    case _2;

    public function getIt(): string
    {
        return match ($this) {
            ExampleEnum::_1 => 'foo',
            ExampleEnum::_2 => 'bar',
        };
    }
}

$id = "_1";
ExampleEnum::$id->getIt();
0

1 Answer 1

6

It's important to distinguish between three different things here:

  • A class property would be declared in a class as static $foo (with optional visibility, type, and default value), and accessed as Example::$foo. Dynamically, you could access that with "variable-variable" syntax, e.g. $name = 'foo'; echo Example::$$name;.
  • A class constant would be declared as const foo = 'value' (with optional visibility), and accessed as Example::foo. From PHP 8.3 onwards, you could access that dynamically with $name = 'foo'; echo Example::{$name};. In earlier versions, there was no direct syntax, but you could use the constant function, as in $name = 'foo'; echo constant("Example::$name");
  • An enum case is declared as case foo (with a value if using a "backed enum"), and it is accessed using the same syntax as a class constant.

In fact, an enum case is treated like a constant in many ways, and a test shows that both the constant function and the PHP 8.3 dynamic constant syntax can be used to look one up, so this will work:

enum ExampleEnum
{
    case _1;
    case _2;

    public function getIt(): string
    {
        return match ($this) {
            ExampleEnum::_1 => 'foo',
            ExampleEnum::_2 => 'bar',
        };
    }
}

$id = "_1";

// PHP 8.3 or later only:
echo ExampleEnum::{$id}->getIt();

// any version supporting enums (PHP 8.0 or later):
echo constant("ExampleEnum::$id")->getIt();

Note that if you use a "backed enumeration", you can associate an arbitrary integer or string with each case, separate from its name, and use the from method to look up a case based on that value.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me out, and indeed works as described. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.