PHP枚举集

时间:2019-03-29 08:16:41

标签: php enums

是否可以像在Delphi中那样在PHP中创建枚举类型集

我想为[必需,只读,...]之类的组件创建一组选项。我见过PHP and Enumerations,但不能完全解决我的问题。

例如,在Delhpi中,您可以执行以下操作:

type enum_options = (required, readonly, ...); // Enumerated type
options_for_my_component := [required, readonly];
...
if (required in options_for_my_component) then
   ...

更多信息,http://www.delphibasics.co.uk/Article.asp?Name=Sets

1 个答案:

答案 0 :(得分:0)

还是有点不确定您在PHP中缺少什么。使用in_array应该非常简单(除非我错过了什么):

abstract class DaysOfWeek
{
  const Sunday = 0;
  const Monday = 1;
  const Tuesday = 2;
  const Wednesday = 3;
  const Thursday = 4;
  const Friday = 5;
  const Saturday = 6;
}

$working_days = [DaysOfWeek::Monday, DaysOfWeek::Tuesday, DaysOfWeek::Wednesday, DaysOfWeek::Thursday, DaysOfWeek::Friday];
$today = (int)date('w');

echo in_array($today, $working_days) ? 'Today is a working day!' : 'Today is a weekend day!';

演示:https://3v4l.org/RJg9B

(您明天可以尝试此演示,它应该说是一个工作日:))