经常需要dropDownList或类似的字段,要取options和值相应的显示项目。
以blog demo为例
/** * @return array post status names indexed by status IDs */ public function getStatusOptions() { return array( self::STATUS_DRAFT=>'Draft', self::STATUS_PUBLISHED=>'Published', self::STATUS_ARCHIVED=>'Archived', ); } /** * @return string the status display for the current post */ public function getStatusText() { $options=$this->statusOptions; return isset($options[$this->status]) ? $options[$this->status] : "unknown ({$this->status})"; }
如果类似的字段非常多(我现在的项目就是这样),会很麻烦。我想将这两个函数放到一个Option类中,这样只要继承一下就可以了。
这样做是否方便可行?还是多写几个函数好,请大家指点。