2011-08-17 11 views
0

내 코드의 일부를 클래스로 이동하는 방법이 확실하지 않습니다.PHP OOP 클래스 도움말

<?php 
class InfoTest { 

     private $info_results; 

     public function __construct() { 
      $dbc = get_dbc(); 
      $info = $dbc->query ("SELECT info_id, info_title FROM text"); 
      if ($dbc->error) { 
       printf("Error: %s\n", $dbc->error); 
      }  
      while ($info_row = $info->fetch_array()) 
      { 
       $info_results[]= $info_row; 
      } 
      $info->free(); 
      $this->info_results = $info_results; 
     } 


     public function setInfo() { 
      $this->info_results = $info_results; 
     } 

     public function getInfo() { 
      return $this->info_results; 
     } 

     public function __destruct() {  
     } 

} 

    ?> 
<?php 
$display = new InfoTest(); 
foreach ($display->getInfo() as $info_row) { 
?> 
<!-- html --> 
<?php echo $info_row['info_title']."</a><br />"; ?> 
<!-- html --> 
Sub-Info: 
<?php 
$dbc = get_dbc(); 
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id'].""); 
if ($dbc->error) { 
     printf("Error: %s\n", $dbc->error); 
} 
$num =$si_title->num_rows; 
$count = 0; 
while ($sub_info = $si_title->fetch_array()) 
{ 
     $sub_info_title = $sub_info['info_title']; 
     if ($count!=$num-1) 
     { 
      echo $sub_info_title." , "; 
      $count++; 
     } 
     else echo $sub_info_title;      
    }     
?> 
<!-- html --> 
<?php } ?> 

하위 정보 (하위 정보 뒤에있는 코드 :)를 클래스로 이동하는 방법을 잘 모르겠습니다. InfoTest 클래스와 같은 클래스에 속해 있습니까? 클래스 자체에 속하지 않습니까?

하위 정보 코드 :

<?php 
$dbc = get_dbc(); 
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id'].""); 
if ($dbc->error) { 
     printf("Error: %s\n", $dbc->error); 
} 
$num =$si_title->num_rows; 
$count = 0; 
while ($sub_info = $si_title->fetch_array()) 
{ 
     $sub_info_title = $sub_info['info_title']; 
     if ($count!=$num-1) 
     { 
      echo $sub_info_title." , "; 
      $count++; 
     } 
     else echo $sub_info_title;      
}     
?> 
+0

하위 정보의 종류? –

+0

SQL 요청이 클래스에 들어가고 HTML/PHP 변수를 표시하는 데 사용되는 루프가 템플릿에 들어갑니다. – Elorfin

+0

우선 당신은 OOP에 대해 연구해야하고 어떻게 작동합니까 .. 구글에서 이것을 발견했습니다. 멋지게 보입니다. http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx 그런 다음 코드를 구조화하는 방법을 알게 될 것입니다. 수업에서. OOP를 사용하는 방법에 대한 PHP 기술 사양은 http://php.net/manual/en/language.oop5.php를 참조하십시오. OOP에는 다음과 같은 코드가 포함되어 있습니다 : P – Catalin

답변

0

클래스에서 당신은 이미 모든 정보가 있습니다. 따라서 SQL 쿼리의 대안은 개인 필드 info_results에있는 특수 ID로 모든 제목을 검색하는 추가 방법 일 수 있습니다. 예컨대 :

public function getInfoTitles($info_id) { 
    $titles = array(); 
    foreach ($this->info_results as $info_row) { 
     if ($info_row['info_id'] == $info_id) 
      $titles[] = $info_row['info_title']; 
     } 
    } 
    return $titles; 
} 

귀하의 하위 정보 코드는 다음과 같습니다

echo implode(', ', $display->getInfoTitles($info_row['info_id'])); 
+0

도움을 주셔서 감사합니다! – markerpower

0

OOP의 일반적인 생각은 데이터 것을 처리 방법과 몇 가지 데이터입니다. 따라서 데이터의 일부가 동일한 방식으로 여러 번 처리된다고 생각되면 데이터와 로직을 캡슐화하지 않는 클래스를 도입하는 것이 좋습니다.

물론 많은 수의 클래스가 있어야하며, 얼마나 많은 클래스가 있어야하며, 서로 상호 작용해야하는지, 비즈니스 로직의 어느 부분이 어떤 클래스에 포함되어야하는지 등에 관한 질문이 많이 있습니다. 보편적이고 항상 정확한 대답은 없습니다. 그 질문에 대답하기위한 몇 가지 일반적인 접근 방식이 개발되었습니다 : the design patterns. 이 주제에 관한 책이 있는데, 가장 잘 알려진 책은 Gang-of-Four (GoF) Design Patterns입니다.

주제에 대한 일반적인 생각입니다. 특별한 경우에 새로운 클래스 ItemInfo을 생성하는 것이 좋습니다. 따라서 InfoTest 클래스는 DB를 쿼리하고이 새 클래스의 인스턴스를 만드는 역할을합니다.

class InfoTest { 

    private $items; 

    public function __construct() { 
     $this->items = new Array(); 
    } 

    private function queryItems($itemId){ 
     $dbc = get_dbc(); 
     $info = $dbc->query("SELECT info_id, info_title FROM text"); 
     if ($dbc->error) { 
      printf("Error: %s\n", $dbc->error); 
     }  
     while ($info_row = $info->fetch_array()) 
     { 
      $item = new ItemInfo($info_row); 
      $this->items[] = $item; 
     } 
     $info->free(); 
    } 

    public function getItems($itemId){ 
    if (empty($this->items)){ 
     $this->queryItems($itemId); 
    } 
    return $this->items; 
    } 

    /* Other functions. */ 
    public function __destruct() {  
    } 
} 

Class ItemInfo{ 
    private $id, $title; 
    function __construct(Array $params){ 
     $this->id = $params['item_id']; 
     $this->title = $params['item_title']; 
    } 

    function getTitle(){ 
     return $this->title; 
    } 

    function toString(){ 
     retirn "I'm item {$this->id}, my title is {$this->title}"; 
    } 
} 

그리고 당신의 코드가 될 것입니다 간단

$item_test = new ItemTest(); 
$items = $item_test->getItems($item_id); 
$titles = array(); 
foreach ($items as $item){ 
    //you may process your items in any way you need 
    $titles[] = $item->getTitle(); 
} 
echo implode(',', $titles); 
+0

도움을 주셔서 감사합니다! – markerpower