Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if enum type?

How can I tell if a variable is of type enum?

I have installed PHP 8.1 on my Ubuntu 20.04. I'm testing the new "enum" types.

Is something like that possible?

is_enum($var)
gettype($var) === 'enum'
like image 301
Richi RM Avatar asked Sep 03 '25 14:09

Richi RM


2 Answers

Someone told me the solution:

if ($var instanceof \UnitEnum) {
   echo 'is enum';
}
like image 132
Richi RM Avatar answered Sep 05 '25 06:09

Richi RM


use enum_exists, for check if is a enum.

if (enum_exists(Item::class)) {
    $myType = Item::Manufactured;
}
like image 26
juliosantos73 Avatar answered Sep 05 '25 05:09

juliosantos73