dqcd84732 2013-09-23 16:50
浏览 42

在哪里找到属性的可接受值[PHP优先但可以考虑用于所有语言]

I have an entity, let's say "customer" and this entity has 3 possible status: active, inactive and deleted.

This 3 status handles 3 labels: Active, Inactive and Deleted and they are used in the form, in the detail and maybe in a list so I we have a common array:

array(
   'active' => 'Active',
   'inactive' => 'Inactive',
   'deleted' => 'Deleted
);

What I normally do is to handle this in a method called getAcceptedStatus that returns this values but then I figured out that I had to make something like this:

$acceptedStatus = $customer->getAcceptedStatus();
echo $acceptedStatus[$customer->getStatus()];

And I do not find it good enough. How do you (would you) handle it? Should I create a method that return directly the label?

  • 写回答

1条回答 默认 最新

  • duanban4769 2013-09-23 17:34
    关注

    Why not make the status an entity in its own right?

    This way you can assign the customers status property an object instance rather then messing around with strings.

    abstract class Status 
    {
      protected $id;
    
      protected $name;
    
      public function getId();
    
      public function setId($id);
    
      public function getName();
    
      public function setName($name);
    
    }
    
    class CustomerStatus extends Status
    {
     protected $label;
    
    
     public function getLabel()
     {
       return $this->label;
     }
    
     public function setLabel($label);
    
    }      
    

    Then type hint in the customer class:

    class Customer {
      // ...
      public function getStatus() {
        return $this->status;
      }
    
      public function setStatus(CustomerStatus $status) {
       $this->status = $status;
      }
    
      // ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化