Php 单功能与多功能
我正在为一个MVC项目开发一个模型。我想知道,一个任务使用多个函数,还是每种处理方式使用一个函数更好。例如,是否最好有以下内容:Php 单功能与多功能,php,codeigniter,function,methods,Php,Codeigniter,Function,Methods,我正在为一个MVC项目开发一个模型。我想知道,一个任务使用多个函数,还是每种处理方式使用一个函数更好。例如,是否最好有以下内容: public function get($identifiers = null, $limit = null, $offset = null) { if ($identifiers != null) { if (is_array($identifiers)) { $this->db->where($ident
public function get($identifiers = null, $limit = null, $offset = null)
{
if ($identifiers != null) {
if (is_array($identifiers)) {
$this->db->where($identifiers);
} else {
$this->db->where($this->_key, $identifiers);
$method = 'row'.($this->_return_array ? '_array' : '');
return $this->db->get($this->_table)->$method();
}
}
if ($limit != null) {
$this->db->limit($limit, $offset || null);
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order);
}
$method = 'result'.($this->_return_array ? '_array' : '');
return $this->db->get($this->_table)->$method();
}
处理多个案例或具有单独功能的,例如
get($id) {}
get_where($where) {}
get_all() {}
诸如此类。独立的职能部门坚持单一责任原则,比一个职能部门尝试做许多事情更为接近。这意味着您将拥有更易于理解、调试、修改和测试的更小的函数。几乎在所有情况下,多个特定功能都比单一功能更好 这取决于这些函数内部的情况。 如果大多数业务逻辑是相同的,只是输入参数不同(比如说,您需要准备不同的参数,但之后逻辑是相同的),那么我将使用单个函数。
在其他情况下,我会执行多个更小的函数-它更易于维护,更容易理解那里发生的事情。我认为第二个版本更好,如果你想要像“干净代码”这样的东西。谢谢,在编写第一个函数后,我觉得这不是正确的处理方法。我想这就是当我试着对我的代码感兴趣时会发生的事情:)