【发布时间】:2022-07-08 02:03:06
【问题描述】:
我有基本的枚举
enum Fruit
{
case APPLE;
case ORANGE;
case BANANA;
}
还有一些使用枚举类型的函数:
function eatFruit (Fruit $fruit)
{
// do stuff
}
和内容未知的变量
$fruit = $_POST['fruit']; // user choosed "MILK"
if (?????) { // how to check if it's fruit?
eatFruit($fruit); // this should not be executed
}
我在documentation 中找不到检查枚举是否包含特定大小写的简单方法。
这样的支持枚举是可能的
enum Fruit
{
case APPLE = 'APPLE';
case ORANGE = 'ORANGE';
case BANANA = 'BANANA';
}
Fruit::from('');
Fruit::tryFrom('');
这可行,但from 在我的第一个示例中不存在于非支持枚举上。
Fatal error: Uncaught Error: Call to undefined method Fruit::from()
【问题讨论】:
-
这能回答你的问题吗? Get Case from enum by string