[英]PHP/Codeigniter Dry Principle on Model functions
Let's say i'm making a web application for a online story sharing site. 假设我正在为在线故事共享站点制作一个Web应用程序。
We have tables: genre,tags,content_warning with only 2 columns as id and name. 我们有表格:genre,tags,content_warning,只有两列作为id和name。 for example: genre = 1 - romance, 2 - action, etc. 例如:类型= 1-浪漫,2-动作等。
I use the above tables to control/define each genre/tags/contentwarnings. 我使用上面的表格来控制/定义每个类型/标签/内容警告。 so whenever a user publishes/uploads their own story they just cant input random tags/genre/contentwarnings. 因此,每当用户发布/上传自己的故事时,他们就无法输入随机标签/体裁/内容警告。
So i'll have these functions in my story_model : 所以我将在我的story_model中使用以下功能:
public function get_genre(){
$genre = array();
$query = $this->db->get('genre');
foreach($query->result() as $row){
$genre[$row->genre_id] = $row->genre_name;
}
return $genre;
}
public function get_tags(){
$tags = array();
$query = $this->db->get('tag');
foreach($query->result() as $row){
$tags[$row->tag_id] = $row->tag_name;
}
return $tags;
}
public function get_content_warnings(){
$content_warning = array();
$query = $this->db->get('content_warning');
foreach($query->result() as $row){
$content_warnings[$row->content_warning_id] = $row->content_warning_name;
}
return $content_warning;
}
Am i correct to say that i am repeating myself in the above 3 functions? 我是否正确地说我要在以上三个功能中重复我自己? so i would write a single code like: 所以我会写一个像这样的代码:
public function sample_get($table){
$data = array();
$query = $this->db->get($table);
foreach($query->result_array() as $row){
$data[$row[$table.'_id']] = $row[$table.'_name'];
}
return $data;
}
to access the above function in my controller i would pass 'genre' or 'tag' or 'content_warning' as the parameter. 要在我的控制器中访问上述功能,我将传递“体裁”或“标签”或“ content_warning”作为参数。
How would i name my above function then? 那我该如何命名以上功能呢? the 3 separate functions are easy to name as they are very direct to the point and you know what the function does by reading its name. 这3个独立的函数很容易命名,因为它们非常直接,您可以通过读取函数的名称来了解其功能。
combining all 3 function into one makes it harder to name the function in a direct way. 将所有3个功能组合为一个功能,就很难直接命名该功能。 I like to name my function as direct as possible so its easy for me to follow. 我喜欢尽可能直接地命名我的函数,这样我就很容易理解。 as in they're more readable. 因为它们更具可读性。
Account_model: Account_model:
public function get_userid($username){
$this->db->select('user_id');
$this->db->from('user');
$this->db->where('username', $username);
$query = $this->db->get();
foreach($query->result() as $row){
$user_id = $row->user_id;
}
return $user_id;
}
story_model: story_model:
public function get_stories($user_id){
$stories = array();
$this->db->select('story_id, story_name');
$this->db->from('story');
$this->db->where('user_id', $user_id);//author
$query = $this->db->get();
foreach($query->result() as $row){
$stories[$row->story_id] = $row->story_name;
}
return $stories;
}
would the above two function warrant a dry-ification? 以上两个功能是否值得干燥处理?
Or let's change the second function to only get the story_id which would coincide with the account_model function to get user_id. 或者让我们更改第二个函数以仅获取story_id,该故事将与account_model函数重合以获取user_id。 would they then be needed to dry? 然后需要干燥吗?
but i get confused as to when do i decide to dry my functions? 但是我对何时决定干我的功能感到困惑? since i'll use alot of get function to retrieve data should i then just opt for a single get function? 因为我将使用很多get函数来检索数据,然后我应该只选择一个get函数?
If you want to use your common, abstract function, but keep well-named public functions, you can take the following approach: 如果要使用公用的抽象函数,但保留命名良好的公共函数,则可以采用以下方法:
/**
* Use the common function internally, but do not expose it publicly
*/
private function sample_get($table){
$data = array();
$query = $this->db->get($table);
foreach($query->result_array() as $row){
$data[$row[$table.'_id']] = $row[$table.'_name'];
}
return $data;
}
/**
* Use well-named functions that are exposed to be used publicly
*/
public function get_content_warnings(){
return $this->sample_get('content_warning');
}
This makes it possible to structure your class internally (through using the keyword private
), but maintain a good interface to be used by other classes (through using public
). 这样就可以在内部构造类(通过使用关键字private
),但保持良好的接口以供其他类使用(通过使用public
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.