如何以正确的方式创建一个类

时间:2014-12-04 21:11:35

标签: php oop

我在Procedural php工作了5年,最近我决定咬紧牙关,进步到OOP。 现在,我正在努力创建一个基本的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 个答案:

答案 0 :(得分:3)

对于初学者来说,你的span()方法似乎没有返回一个对象。您需要在span()方法中创建span对象并将其返回。 span对象应该包含widget()方法,而widget()方法应该只将数据分配给span对象。应创建所有跨度对象,以便在您创建的网格对象中进行参考。然后,$ grid-&gt; 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.

的事情