2014-12-04 1 views
0

저는 Procedural php에서 5 년입니다. 최근에 나는 총알을 물기로 결정하고 OOP로 진행합니다. 지금 나는 이것이 내가 지금까지 무엇을 가지고이 클래스는 그리드 내가이올바른 방법으로 클래스를 만드는 법

$grid = new Grid(); 
// a span is an area the father and the widget is the son, so spans have widgets inside. 

// a span a width of 6 
$span1 = $grid->span("6"); 

// a new widget inside span1 
$span1->widget("icon", "title1", "content1"); 
// another new widget inside span1 
$span1->widget("icon", "title2", "content2"); 

// a span a width of 4 
$span2 = $grid->span("4"); 

// a new widget inside span2 
$span2->widget("icon", "title1", "content1"); 
// another new widget inside span2 
$span2->widget("icon", "title2", "content2"); 

// echo /return results 
$grid->render(); 

을 할 수 있도록하고자하는 반응 관리자 템플릿 을 관리하는 기본 UI 클래스를 만들 triyng 만 해요 나는 어떻게 처신해야할지 모르겠다.

class Grid{ 
    private $content; 
    private $innerContent; 
    private $spanResult; 
    private $widgetResult; 

    function span($size){ 
     $this->size = $size; 
     $output = '<div class="span' . $size . '">'; 
      $output .= $this->innerContent; 
     $output .= '</div>'; 

     $this->spanResult .= $output; 
    } 

    function widget($icon, $title, $content){ 
     $output = '<div class="widget widget-nopad">'; 
      $output .= '<div class="widget-header"> <i class="icon-' . $icon . '"></i>'; 
       $output .= '<h3>' . $title . '</h3>'; 
      $output .= '</div>'; 
      $output .= '<div class="widget-content">'; 
       $output .= $content; 
      $output .= '</div>'; 
     $output .= '</div>'; 

     $this->widgetResult = $output; 
    } 

    function render(){ 

    } 
} 

감사합니다.

+1

코드 검토 –

+0

에 게시해야합니다. 지금 막 거기에 게시 된 사람이 많지 않습니다. –

+1

이 질문은 [code review] (http://codereview.stackexchange.com) – sjagr

답변

3

처음에는 span() 메서드가 객체를 반환하지 않는 것처럼 보입니다. span() 메서드 내에서 span 객체를 만들어 반환해야합니다. span 객체는 그 안에 widget() 메소드를 가져야하고 widget() 메소드는 span 객체에만 데이터를 할당해야합니다. 생성 된 모든 스팬 객체는 작성한 그리드 객체 내에 참조 용으로 저장해야합니다. 그런 다음 $ grid-> render() 메서드는 $ span 개체를 반복하여 적절히 출력해야합니다.

편집 : 다음은 내가 생각하고있는 기본적인 사용 예입니다. 그리드 사용을 피하고 대신이 예제를 읽고 그것이 무엇을하는지 보길 원했습니다.

<?php 
// Table class to manage the table object 
class Table 
{ 
    // Init the row collection 
    private $rows; 

    // Function to create a row and save it in the table object. 
    function row() 
    { 
     // Create a row object. 
     $row = new Row; 

     // Save this row object in this table object. 
     $this->rows[] = $row; 

     // Return the row object for method chaining 
     return $row; 
    } 

    // Function to render the table. 
    function render() 
    { 
     // For each row in this table object... 
     foreach($this->rows as $row) 
     { 
      // Var dump for temporary output! 
      var_dump($row->getData()); 
     } 
    } 
} 

// Create a row class to manage the row object 
class Row 
{ 
    // Init the data array to store this row's content. 
    private $data; 

    // Function to add a string to the data array of this particular row object. 
    function addData($string) 
    { 
     // Append string to this row's data 
     $this->data[] = $string; 
    } 

    // Basic function to return data specific to this row's object 
    function getData() 
    { 
     // Return this row's data! 
     return $this->data; 
    } 
} 

// Create the table object 
$table = new Table; 

// Create your first row by using the table object and it's row method. 
// This will create the row object but also save it's reference within the table object. 
// Once we have the row object, use it's addData method to add a string. 
$row = $table->row(); 
$row->addData('Row One Data'); 

// Same as row one but with different data. 
$row2 = $table->row(); 
$row2->addData('Row Two Data'); 

// Render this table object! 
$table->render(); 
?> 

이 출력됩니다 : 보조 노트로

array(1) { [0]=> string(12) "Row One Data" } array(1) { [0]=> string(12) "Row Two Data" } 

, 나는 현재 A.B의 대답에 동의 . 이 정적 메서드를 기반으로해서는 안됩니다 믿습니다. 앞으로 어떤 데이터로든 사용자 정의 그리드를 만들거나 동일한 페이지에 여러 개의 다른 그리드를 출력 할 수 있기 때문에 계속 작업을 진행할 것입니다. 나는 다른 누군가를 설득력있게 설득합니다. 내 생각 프로세스는 여기에 비슷한 것을 시도하고 있다는 것입니다. Magento's Grid.

+0

에 관한 것이기 때문에 주제와는 거리가 먼 것처럼 보입니다. 나는 이것이 oop 커뮤니티에서 누구에게나 의미가 있음을 확신합니다. 예제를 보여 주시겠습니까? –

+0

답변이 업데이트되었습니다. 의견을 듣고 도움이 필요하면 알려주세요. –

+0

이번에는 간단한 설명을 덧붙여 코멘트가 업데이트되었습니다. –

관련 문제