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'
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'
Someone told me the solution:
if ($var instanceof \UnitEnum) {
echo 'is enum';
}
instanceof BackedEnum
. –
Eyeopener use enum_exists, for check if is a enum.
if (enum_exists(Item::class)) {
$myType = Item::Manufactured;
}
enum_exists($var::class)
–
Pendulous To check if a $var
is a BackedEnum
, you can use this code:
if ($var instanceof \BackedEnum) {
echo 'is a backed enum';
}
Read more on documentation of PHP about BackedEnum
.
You also can check it through reflection:
var_dump(
(new ReflectionClass($object::class))
->isEnum()
);
© 2022 - 2024 — McMap. All rights reserved.