我怎样才能简化这个PHP?

发布于 2024-08-13 20:36:33 字数 997 浏览 4 评论 0原文

我有以下 PHP 模型函数。 我知道我在重复自己。

无论如何我可以简化这段代码吗?

function getTopMenus(){
     $data[0] = 'root';
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }  

function getheadMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }  
function getrootMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }

I have the following model functions with PHP.
I know I am repeating myself.

Is there anyway I can simplify this code?

function getTopMenus(){
     $data[0] = 'root';
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }  

function getheadMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }  
function getrootMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤檠 2024-08-20 20:36:33

我可以看到您可以尝试的一种简化,使用传递引用将事物分解为函数:

function prepareMenu(&$data) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();
}

function getTopMenus() {
    $data[0] = 'root';
    prepareMenus($data);
    return $data;
}

function getRootMenus() {
    prepareMenus($data);
    return $data;
}

还可以使用 按引用传递变量函数来分解中间的部分。可能会减少重复,但可能会也可能不会被视为“简化”。

编辑这就是我的意思。该代码未经测试。

function getMenus(&$data, $appendFunc) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $appendFunc(&$data, $row);
       }
    }
    $Q->free_result(); 
}

function appendTopMenu(&$data, $row) {
    $data[$row['id']] = $row['name'];
}

function appendHeadMenu(&$data, $row) {
    $data[] = $row;
}

function getTopMenus() {
    $data[0] = 'root';
    getMenus($data, "appendTopMenu");
    return $data; 
}  

function getheadMenus() {
    getMenus($data, "appendHeadMenu");
    return $data; 
 } 

function getrootMenus() {
    getMenus($data, "appendTopMenu");
    return $data;  
}

I can see one simplification you might try, using pass-by-reference to factor things out into a function:

function prepareMenu(&$data) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();
}

function getTopMenus() {
    $data[0] = 'root';
    prepareMenus($data);
    return $data;
}

function getRootMenus() {
    prepareMenus($data);
    return $data;
}

There's also the possibility of using pass-by-reference and variable functions to factor out the part in the middle. May reduce duplication, but may or may not be considered 'simplifying'.

EDIT Here's what I mean. This code is untested.

function getMenus(&$data, $appendFunc) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $appendFunc(&$data, $row);
       }
    }
    $Q->free_result(); 
}

function appendTopMenu(&$data, $row) {
    $data[$row['id']] = $row['name'];
}

function appendHeadMenu(&$data, $row) {
    $data[] = $row;
}

function getTopMenus() {
    $data[0] = 'root';
    getMenus($data, "appendTopMenu");
    return $data; 
}  

function getheadMenus() {
    getMenus($data, "appendHeadMenu");
    return $data; 
 } 

function getrootMenus() {
    getMenus($data, "appendTopMenu");
    return $data;  
}
葵雨 2024-08-20 20:36:33

为什么不将参数传递到函数中并将它们放在“where”和“get”方法中?

Why not pass parameters into your function and place them in the 'where' and 'get' methods?

黯然#的苍凉 2024-08-20 20:36:33

我不知道你的数据库和查询类是什么样的,但我会首先开始改进它们。将“array fetch”和“hash fetch”函数添加到查询类中:

 class Query ...

        function as_array() {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[] = $row;
           $this->free_result();  
           return $data;
         }
        function as_hash($key = 'id') {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[$row[$key]] = $row;
           $this->free_result();  
           return $data;
         }

使“db->where()”返回自身

     class DB
          function where(...) {
              stuff
              return $this;

一旦有了这个,您的客户端函数就会变得微不足道:

function getTopMenus() {
     $data = $this->db->where('parentid',0)->get('menus')->as_hash();
     $data[0] = 'root';
     return $data;
 } 

function getheadMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_array();
 }  

function getrootMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_hash();
}

I don't know what your db and query classes look like, but i'd start improving these in the first place. Add "array fetch" and "hash fetch" functions to the query class:

 class Query ...

        function as_array() {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[] = $row;
           $this->free_result();  
           return $data;
         }
        function as_hash($key = 'id') {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[$row[$key]] = $row;
           $this->free_result();  
           return $data;
         }

Make 'db->where()' return itself

     class DB
          function where(...) {
              stuff
              return $this;

Once you have this, your client functions become trivial:

function getTopMenus() {
     $data = $this->db->where('parentid',0)->get('menus')->as_hash();
     $data[0] = 'root';
     return $data;
 } 

function getheadMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_array();
 }  

function getrootMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_hash();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文