dpruwm6206 2017-09-13 23:12
浏览 89
已采纳

表示PHP类中的状态数组

I'm not entirely sure the best way to handle a status field that relates to integers in a database table within a class.

Say we have the following:

$status = array(1 => 'Active', 2 => 'Inactive', 3 => 'Cancelled');

I'm thinking for clarity in the system, I want class constants and a getter for retrieving the status in an associative array, and so the class uses something better defined than "1" or "2".

 class User {
  const USER_STATUS_ACTIVE = 1;
  const USER_STATUS_INACTIVE = 2;
  const USER_STATUS_CANCELLED = 3;

  public function getStatusList() {
   return array(User::USER_STATUS_ACTIVE => 'Active',User::USER_STATUS_INACTIVE => 'Inactive',User::USER_STATUS_ACTIVE => 'USER_STATUS_CANCELLED');
  }
 }

This then allows setting the status using the constants with:

class User {
 private $status_id;

 const USER_STATUS_ACTIVE = 1;
 const USER_STATUS_INACTIVE = 2;
 const USER_STATUS_CANCELLED = 3;

 static public function statusList() {
  return array(User::USER_STATUS_ACTIVE => 'Active',User::USER_STATUS_INACTIVE => 'Inactive',User::USER_STATUS_CANCELLED => 'Cancelled');
 }

 public function setStatus($status_id) {
  try {
   if (!key_exists($status_id, User::statusList())) throw new Exception('Invalid status ID');
   $this->status_id = $status_id;
  }
  catch (Exception $e) {
   die($e);
  }
 }
}

$a = new User();
$a->setStatus(User::USER_STATUS_ACTIVE);

Alternately methods can be created for setActive(), setInactive(), setCancelled().

But what I'm trying to figure out is how to best handle the actual status values.

Would it be better to break the statuses out to a static class?

class User {
 private $status_id;

 public function setStatus($status_id) {
  try {
   if (!key_exists($status_id, UserStatuses::statusList())) throw new Exception('Invalid status ID');
   $this->status_id = $status_id;
  }
  catch (Exception $e) {
   die($e);
  }
 }
}

class UserStatuses {
 const USER_STATUS_ACTIVE = 1;
 const USER_STATUS_INACTIVE = 2;
 const USER_STATUS_CANCELLED = 3;

 static public function statusList() {
  return array(UserStatuses::USER_STATUS_ACTIVE => 'Active',UserStatuses::USER_STATUS_INACTIVE => 'Inactive',UserStatuses::USER_STATUS_CANCELLED => 'Cancelled');
 }
}

Or is there something entirely different that would be better?

  • 写回答

2条回答 默认 最新

  • duanji9677 2017-09-14 13:07
    关注

    Using the ReflectionClass

    I like you second example; it's a very simple and effective way to make an enum type (sort of). Actually, some of the pretty big php ORMs out there, like Doctrine operate using a similar pattern. The one thing I like to do to improve scalability is to use the ReflectionClass. This will allow you to add new values in the form of constants without having to change the function that returns the list of values. I would also refactor the code to check if the value is valid in the enum class as well to keep a better separation of concerns between the two classes

    class User {
        private $status_id;
    
        public function setStatus($status_id) {
            if (UserStatuses::isStatus($status_id)) {
                $this->status_id = $status_id;
            }
            else {
                throw new Exception('invalid status');
            }
        }
    }
    
    class UserStatuses {
        const USER_STATUS_ACTIVE = 1;
        const USER_STATUS_INACTIVE = 2;
        const USER_STATUS_CANCELLED = 3;
    
        public static function statusList() {
            $oClass = new ReflectionClass(__CLASS__);
            return $oClass->getConstants();
        }
    
        public static function isUserStatus($int) {
            return in_array($int, self::statusList());
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么