2014-12-04 43 views
0

我在Procedural php 5年,最近我决定硬着头皮,进展到OOP。 现在我triyng创建一个基本的UI类来管理一个负责任的管理模板 此类网格 我希望能够做到这一点如何创建一个类正确的方式

$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(); 

这是我到目前为止,但我不知道如何过关。

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

这应该是在代码审查 – 2014-12-04 21:13:19

+0

我在那里发布,刚刚在那里,没有多少人在那里。 – 2014-12-04 21:17:33

+1

这个问题似乎是脱离主题,因为它是关于[代码评论](http://codereview.stackexchange.com) – sjagr 2014-12-04 21:39:37

回答

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社区,但不是我。 你能给我举个例子吗? – 2014-12-04 21:25:33

+0

已更新的答案,让我知道您的想法以及是否需要更多帮助。 – 2014-12-04 21:38:12

+0

再次更新,这次有了一些简短的解释。 – 2014-12-04 21:47:05

相关问题