2010-08-05 35 views
0

我想在我們的網站上列出我的投資組合,我寫了一個課程,客戶端設置和獲取有關每個客戶的基本知識的參數,即:客戶名稱,網站,它的特色,是否發佈,爲他們做,等等。PHP類是上市內容的有效解決方案嗎?

這是相當原始的,但我正確地認爲最好爲每個客戶端創建一個對象(並保持每個數據在一起),而不是爲每個參數創建數組,並從這些數組中調用相應的元素每次?

這是我的課,我知道它仍然是原始的,但我也可能創建一些其他功能,所以現在我只是使用基本的getter和setter來獲取或設置每個客戶端的參數。

<?php 

class Client { 
    private $name = ""; 
    private $items = array(); 
    private $website = ""; 
    private $featured = false; 
    private $published = false; 
    private $type = array(); 
    private $filename = null; 
    private $extension = null; 

    public function __set($name, $value) { 
     //echo "Setting ".$name." to value: ".$value."\n"; 
     $this->{$name} = $value; 
    } 

    public function __get($name) { 
     //echo "Getting ".$name.": ".$this->{$name}; 
     return $this->{$name}; 
    } 

    public function __isset($name) { 
     return isset($this->{$name}); 
    } 

    public function __unset($name) { 
     unset($this->{$name}); 
    } 
} 

?> 

我仍在計劃我可能想要添加到課程中的功能,但現在就是這樣。

這裏是我的其他代碼我使用創建每個客戶對象:

<?php 

// create Client object for every client 

$files = array(); 
// files to take out of file listing, I'm developing on Mac, i.e. ._DS_Store file 
$bad_files = array(".","..","._DS_Store"); 
$portfolio = "portfolio"; 
$images = "images"; 
$details = "details"; 
$thumbs = "thumbs"; 

// get all *.txt files listed in portfolio, so a client will not be added to portfolio without the necessary details. 
if (is_dir("$images/$portfolio")) { 
    if (is_dir("$images/$portfolio/$details")) { 
     $files = scandir("$images/$portfolio/$details"); 

     sort($files); 
    } 
} 
$files = array_diff($files, $bad_files); 
sort($files); 

// keeps a list of all clients 
$clients = array(); 

foreach ($files as $file) { 
    $value = file_get_contents("$images/$portfolio/$details/$file"); 

    $newClient = new Client(); 

    $filename = explode(".",$file); 
    $newClient->filename = $filename[0]; 
    $client_image = glob("$images/$portfolio/$images/".$newClient->filename.".*"); 
    $newClient->image = $client_image[0]; 
    $client_thumb = glob("$images/$portfolio/$thumbs/".$newClient->filename.".*"); 
    $newClient->thumb = $client_thumb[0]; 

    $client_items = array(); 
    $client_type = array(); 

    // gets variables from text file contents and explode string to array [key=value] values 
    $content = explode("&",$value); 
    for ($j=0; $j<count($content); $j++) { 
     $client = explode("=", $content[$j]); 
     if (strpos($client[0],"name") !== false) $newClient->name = $client[1]; 
     if (strpos($client[0],"items") !== false) $client_items = $client[1]; 
     if (strpos($client[0],"website") !== false) $newClient->website = $client[1]; 
     if (strpos($client[0],"featured") !== false) $newClient->featured = $client[1]; // show on frontpage 
     if (strpos($client[0],"published") !== false) $newClient->published = $client[1]; // show at all 
     if (strpos($client[0],"type1") !== false) $client_type[] = $client[1]; // show for specific type, eg. business card, website 
     if (strpos($client[0],"type2") !== false) $client_type[] = $client[1]; // show for specific type, eg. business card, website 
    } 

    // these parameters need array type values 
    $newClient->type = $client_type; 
    $newClient->items = explode(", ",$client_items); 

    // adds client to list of clients 
    $clients[] = $newClient; 
} 

?> 

這裏是我用來輸出每個客戶的旗幟和細節代碼:

<div id="banner_content"> 
    <? 
     foreach ($clients as $client) { 
     // client must be published to show at all 
      if ((($page == "home" && $client->featured) || $page != "home") && $client->published) { 
    ?> 
    <div class="banner_container"><img src="<? echo $client->image; ?>" width="809" height="324" alt="<? echo $client_name; ?>" title="<? echo $client_name; ?>" /> 
     <div class="banner_details"> 
      <div class="client_name">Client: <b><? echo (!empty($client->name) ? $client->name : "Unknown"); ?></b></div> 
      <div class="client_items"><? echo (!empty($client->items) ? "Items: <b>".join(", ",$client->items)."</b>" : ""); ?></div> 
      <div class="client_website"><? echo (!empty($client->website) ? "Website: <b><a href=\"http://".strtolower($client->website)."\">".$client->website."</a></b>" : ""); ?></div> 
     </div> 
    </div> 
    <? 
    } 
} 
?> 
</div> 

任何幫助或建議將不勝感激。提前致謝。

//編輯

我忘了提,我居然寫了類,因爲會有一個投資組合頁面,其中將包含有關客戶的不僅僅是上述信息的詳細信息。我知道一個班級只是在橫幅中列出圖片是有點矯枉過正的。

回答

0

我對web開發沒有任何經驗,但在這裏使用一門課是絕對好的編程習慣。
管理多個平行陣列很麻煩。例如,無論何時插入新項目或需要移動項目,都需要記住每個數組。此外,在這裏使用類可以使您的代碼更有組織,因此更易於維護。
我認爲這當然值得實施這個類的開銷,尤其是一旦你已經掌握了在PHP中使用類的過程!

+0

Aaahhh,dankie Bernhard! – 2010-08-05 13:34:31

+0

可以通過數組跟蹤多個Client對象嗎? – 2010-08-05 13:41:00

0

如果一個類是一個有效的解決方案取決於你如何實現它。這不像你的代碼會奇蹟般地被增強,因爲你對某個東西進行了類聲明。

使用類的關鍵思想之一是封裝狀態和責任。因此,封裝與客戶端相關的狀態的類Client是一個好主意。給它的方法呈現爲ListTable不是。我知道,你沒有這樣做,但你也沒有使用一個有責任將客戶端呈現爲列表或表格的類。您也沒有課程來查找相關的客戶端文件。您的第二個發佈代碼塊是一個程序腳本。它不使用OOP。確定問題並將其分解爲小(可測試)單元。將它們合併到適當的類中。

有一個不錯的series of articles by Lorna Jane Mitchell about PHP5 and OOPa number of good Q&A on StackOverflow as well。你也想看看what Design Patterns are。和here is a shameless self-plug

相關問題