'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'
Solution 1:[1]
Someone told me the solution:
if ($var instanceof \UnitEnum) {
echo 'is enum';
}
Solution 2:[2]
use enum_exists, for check if is a enum.
if (enum_exists(Item::class)) {
$myType = Item::Manufactured;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Julio SANTOS |