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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题