duanfu1942 2013-10-27 18:50 采纳率: 100%
浏览 77

PHP和枚举麻烦[重复]

This question already has an answer here:

I've looked everywhere and cannot seem to figure this out. My issue is that I am trying to use the 'enum' for rank that is in my database but keep getting errors. What I have below I'd like to be able to display in a specific div depending on what rank a member is. This is what I have so far, where am I going wrong? If someone could shed some light on if this is even correct or what I need to do to make this work, I'd greatly appreciate it.

PHP:

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:

<p><?php echo $rank ?></p>
</div>
  • 写回答

1条回答 默认 最新

  • dqnqpqv3841 2013-10-27 19:40
    关注

    It looks like you are trying to use the SplEnum class to make an enum. In order to have such an enum, you need your enum class to extend the SplEnum abstract class.

    class Fruit extends SplEnum
    {
        const __default = self::Apple; // default value; could also be __default = 0;
        // values
        const Apple = 0;
        const Orange = 1;
    }
    

    I haven't tested it, but I'm fairly certain that constants in an enum are case-sensitive. That is, if you define an enum Fruit like above, you can use Fruit::Orange but not Fruit::ORANGE. I think the normal convention is to use all UPPERCASE, since they are constants.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度