There is a lot of type checking functions in PHP (is_string
, is_resource
etc) but there is no function to check if variable is an Enum or a Case. How to check it properly?
Asked
Active
Viewed 1,315 times
-1

Grzegorz Adam Kowalski
- 5,243
- 3
- 29
- 40
-
https://www.php.net/manual/en/function.enum-exists.php – mickmackusa Apr 19 '22 at 23:37
1 Answers
1
You can check that it implements the UnitEnum interface.
if ($variable instanceof UnitEnum) echo "it's an enum.";
According to the PHP manual, this interface exists specifically for type checking.

Don't Panic
- 41,125
- 10
- 61
- 80
-
This is a duplicate question and your advice is found on the dupe target. – mickmackusa Apr 19 '22 at 23:38