문제

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; 
 }
도움이 되었습니까?

해결책

통과 회의를 사용하여 기능을 고려해 볼 수있는 하나의 단순화를 볼 수 있습니다.

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;  
}

다른 팁

매개 변수를 기능에 전달하여 '여기서'및 'get'방법에 배치하지 않겠습니까?

DB 및 쿼리 클래스의 모습은 모르겠지만 처음에는이를 개선하기 시작합니다. "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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top