关于数据库:PHP和枚举问题

PHP and Enum Troubles

本问题已经有最佳答案,请猛点这里访问。

我到处都找过了,似乎找不到答案。我的问题是,我正在尝试使用"枚举"来表示数据库中的排名,但不断出错。我下面有什么我想能够显示在一个特定的分区取决于什么等级的成员。这就是我到目前为止所拥有的,我哪里出错了?如果有人能解释一下这是否正确,或者我需要做些什么来完成这项工作,我会非常感激。

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Rank
{
  // If no value is given during object construction this value is used
  const __default = 1;
  // Our enum values
  const Registered       = 1;
  const Legend           = 2;
  const Inactive         = 3;
  const Greenhorn        = 4;
  const FullMember       = 5;
  const JuniorAdmin      = 6;
  const SeniorAdmin      = 7;
  const Leader           = 8;
  const Founder          = 9;
  const OriginalFounder  = 10;
}

$registered     = new Rank(Rank::REGISTERED);
$legend         = new Rank(Rank::LEGEND);
$inactive           = new Rank(Rank::INACTIVE);
$greenhorn          = new Rank(Rank::GREENHORN);
$fullmember     = new Rank(Rank::FULLMEMBER);
$junioradmin        = new Rank(Rank::JUNIORADMIN);
$senioradmin        = new Rank(Rank::SENIORADMIN);
$leader         = new Rank(Rank::LEADER);
$founder            = new Rank(Rank::FOUNDER);
$originalfounder    = new Rank(Rank::ORIGINALFOUNDER);
$fail              = 1;

function responsibilities(Rank $rank)
{
  if (Rank::REGISTERED == $rank) {
    echo"Post something about being a registered member.
"
;
  } elseif (Rank::LEGEND == $rank) {
    echo"Post something about being Legend.
"
;
  } elseif (Rank::INACTIVE == $rank) {
    echo"Post something about being Inactive./n";
  } elseif (Rank::GREENHORN == $rank) {
    echo"Post something about being a Greenhorn./n";
  } elseif (Rank::FULLMEMBER == $rank) {
    echo"Post something about being a Full Member./n";
  } elseif (Rank::JUNIORADMIN == $rank) {
    echo"Post something about being a Junior Admin./n";
  } elseif (Rank::SENIORADMIN == $rank) {
    echo"Post something about being a Senior Admin./n";
  } elseif (Rank::LEADER == $rank) {
    echo"Post something about being a Leader./n";
  } elseif (Rank::FOUNDER == $rank) {
    echo"Post something about being a Founder./n";
  } elseif (Rank::ORIGINALFOUNDER == $rank) {
    echo"Post something about being an Original Founder./n";
  }
}

responsibilities($registered);
responsibilities($legend);
responsibilities($inactive);
responsibilities($greenhorn);
responsibilities($fullmember);
responsibilities($junioradmin);
responsibilities($senioradmin);
responsibilities($leader);
responsibilities($founder);
responsibilities($originalfounder);

responsibilities($fail);

HTML:

1
2
3
<p>
<?php echo $rank ?>
</p>


看起来您正在尝试使用splenum类来创建枚举。为了拥有这样的枚举,需要您的枚举类来扩展SplEnum抽象类。

1
2
3
4
5
6
7
class Fruit extends SplEnum
{
    const __default = self::Apple; // default value; could also be __default = 0;
    // values
    const Apple = 0;
    const Orange = 1;
}

我还没有测试过它,但我相当确定枚举中的常量是区分大小写的。也就是说,如果您像上面那样定义枚举Fruit,那么可以使用Fruit::Orange,但不能使用Fruit::Orange。我认为正常的惯例是使用全部大写,因为它们是常量。