Getting an PHP enum from dynamic name

48
May 25, 2022, at 10:50 AM

I´m trying to create an php 8.1 enum from a dynamic name. Seems not to be possible. So given an enum

enum Foo {
    case bar;
}

The following works of course: Foo::bar While this doesn´t:

$name = "bar";
Foo::${$name}

This results in: Access to undeclared static property App\Console\Commands\Foo::$bar

I tried various tricks here, to no avail. It seems not to be possible to get an enum instance from a dynamic name. My custom and quick workaround looks like this, a static factory:

/**
 * @throws \Exception
 */
public static function fromName(string $name) : self {
    foreach(Foo::cases() as $enum){
        if($enum->name === $name){
            return $enum;
        }
    }
    throw new \Exception("Not a valid enum name");
}

I could put this in a trait and inherit this in all my enums, yes, problem solved.

My question is: am I missing something here? Is it really not possible to instantiate an enum with native php methods? Or am I thinking in the wrong direction?

The pre php8.1 class-as-enum composer packages used to have those convenience methods, see https://github.com/bensampo/laravel-enum So why is this pretty common case not part of the specification (just curious)?

Answer 1

Could you maybe do something like this?


enum Status {
  case OK;
  case DENIED;
}
$name = "OK";
$reflection = new \ReflectionEnum(Status::class);
if ( $reflection->hasConstant( $name ) ) {
  var_dump($reflection->getConstant( $name ));
}

Output: enum(Status::OK)

Answer 2

Alternatively, you could make your enum backed and use the ::from method.

// note: type-hinting required
enum Status: int
{
    case OK = 200;
    case ERROR = 500;
}
$statusValue = 200;
$status = Status::from($statusValue);
(returns Status::OK)

If you're using Laravel casting, I believe it requires you to use a backed enum anyway. And in your model you just need to add that cast on your model

...
protected $casts = [
    'status' => \App\Emums\Status::class,
];
Rent Charter Buses Company
READ ALSO
PHP categories showing only one item

PHP categories showing only one item

I'm trying to create a categories list, where user will be able to choose item with certain category, but as soon as I'm opening any of the categories, where "soon = '2'", there is only one item coming upEverything else works

137
PHP can you convert unicode from within a string

PHP can you convert unicode from within a string

I have a php string with the following

98
Invisible Character Unicode

Invisible Character Unicode

Does anyone know if there is any invisible character other than the space? like a website using (Example: This Invisible Character)Is it possible to programmatically add some such characters that are not shown by any editor, i'm trying by unicode but when ever i paste it so it takes same unicode...

81
How to add value in the second/child layer of an array [duplicate]

How to add value in the second/child layer of an array [duplicate]

I'm trying to add a value media_type to every child of the arrayThe code I'm using is:

75