I have basic enum
enum Fruit
{
case APPLE;
case ORANGE;
case BANANA;
}
and some function that uses typing with that enum:
function eatFruit (Fruit $fruit)
{
// do stuff
}
and variable with unknown content
$fruit = $_POST['fruit']; // user choosed "MILK"
if (?????) { // how to check if it's fruit?
eatFruit($fruit); // this should not be executed
}
I cannot find in documentation simple way to check if enum contains specific case.
It is possible with backed enums like that
enum Fruit
{
case APPLE = 'APPLE';
case ORANGE = 'ORANGE';
case BANANA = 'BANANA';
}
Fruit::from('');
Fruit::tryFrom('');
This will work, but from does not exist on non-backed enums form my first example.
Fatal error: Uncaught Error: Call to undefined method Fruit::from()