2010-05-27 111 views
0

我正在學習有關課程,現在在PHP和他們的例子是如..我應該把什麼東西放進課堂,我不應該做什麼?

class table { //makes a table 
    private $tag ; 
    function Begin($border=0, $align="center", $width='100%', $cellpadding=2, 
     $cellspacing=2, $class='', $id='', $bgcolor='', $style='') { 
     $this->tag = '<table ' ; 
     if ($align)    $this->tag .= 'align="' . $align . '" ' ; 
     if ($width)    $this->tag .= 'width="' . $width . '" ' ; 
     if ($border > 0)   $this->tag .= 'border="' . $border . '" ' ; 
     if ($cellpadding > 0)  $this->tag .= 'cellpadding="' . $cellpadding . '" ' ; 
     if ($cellspacing > 0)  $this->tag .= 'cellspacing="' . $cellspacing . '" ' ; 
     if ($class)    $this->tag .= 'class="' . $class . '" ' ; 
     if ($id)     $this->tag .= 'id="' . $id . '" ' ; 
     if ($bgcolor)    $this->tag .= 'bgcolor="' . $bgcolor . '" ' ; 
     if ($style)    $this->tag .= 'style="' . $style . '" ' ; 
     $this->tag .= ">" ; 
     return $this->tag ; 
    } 

然後你只需實例並通過

$table =new table; 
$table->$table($border=2, $align='center', etc); 

我應該編碼喜歡這個地方HTML做一個表,CSS是在課堂上? 我覺得以這種方式製作表格和表格會更困惑,然後實際上只是打字。我只應該像驗證,從數據庫中獲取數據,以及類中的邏輯東西嗎?

我應該使用什麼類而不是?

+1

你在哪裏得到這個例子?誰寫的,應該被槍殺。 – 2010-05-27 20:40:15

回答

2

你可以這樣做......我個人不會。

使用對象domain objects。舉個例子,想象一下Facebook的源代碼。我敢肯定,在某處Facebook的源代碼,你會發現如下:

class Friend 
{ 
    // ... 
} 

......同樣,如此用PHP編寫的,你會發現這樣的事情:

class Question 
{ 
    var $originalPoster; 
    var $answers = array(); 
    // ... 

    function close() 
    { 
     // ... 
    } 
    // ... 
} 

使用對象來封裝複雜的想法和概念。在SO上有很多與問題相關的信息,但是你可以將這些信息封裝在一個對象中。

1

對象可以代表什麼你想要它們,但它主要取決於你正在做什麼樣的應用程序。以您的HTML <table>類爲例,如果您的PHP軟件不太關心HTML,那麼爲HTML元素創建PHP類沒有多大意義。

想想你的應用程序。它涉及哪些對象?例如,給定一個作者的博客,可以有帖子,評論,分類和標籤。您想要的相應類自然會是Post,Comment,CategoryTag

希望這是有道理的(這裏有點晚了......)。

+0

是的,它確實!謝謝。現在我有一個配置文件,索引,張貼。所以我認爲我會爲每個人做一個課。 – jpjp 2010-05-27 20:32:03

2

也許這會清理它。最好擴展這個類來封裝整個表,並使用重載,這樣你甚至不需要跟蹤任何東西。你真正需要關心的是HTML驗證,使用有效的HTML表格屬性。事實上,正如前面所說,物體只是讓你更容易概念化你想要做什麼以及你想如何組織你的想法。

使用以下解決方案,您不需要關注對象構造和默認值,就像使用html驗證一樣。在用一個html表格元素參數構建它之後(如果你還想),你可以在之後用參數重載對象。您也可以輕鬆添加行。完全沒有經過測試,但過於簡化...

 
Class My_table { 
    private $table_def = array(); 
    public $rows = array(); 

    function __construct($attributes_array) { 
     if (is_array($attributes_array)) { 
     foreach ($attributes_array as $key => $val) { 
      $this->__set($key, $val); 
      } 
     } 
     } 

    function __set($attribute, $value) { 
     $this->table_def[$attribute] = $value; 
     } 

    function draw() { 
     // write table opening tag 
     echo "<table"; 
     foreach($table_def as $key => $val) { 
     echo " $key='$val'"; 
     } 
     echo ">"; 
     // write all rows out 
     foreach($rows as $current_row) { 
      echo "<tr>"; 
      foreach($current_row as $field_name => $field) { 
      echo "<td>$field</td>"; 
      } 
      echo "</tr>"; 
     ) 
     // finish table 
     echo "</table>"; 
     } 
} 

$table = new My_table(array(
    "style" => "boarder: solid thin gray;", 
    "width" => "100px" 
    )); 
$table->height = "200px"; 
$table->rows[] = array("name" => "someone", "email" => "[email protected]") 
$table->draw(); 
+0

另外,當然,完全沒有必要爲了製作類似網格的表格而創建一個類,除非您有幾十頁的網格狀表格。即使如此,如果您需要在您的站點上標準化表格樣式,那麼這就是CSS的用途。 – 2010-05-27 21:57:32

+0

謝謝!好例子 – jpjp 2010-05-28 00:31:32