我想在我们的网站上列出我的投资组合,我写了一个课程,客户端设置和获取有关每个客户的基本知识的参数,即:客户名称,网站,它的特色,是否发布,为他们做,等等。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>
任何帮助或建议将不胜感激。提前致谢。
//编辑
我忘了提,我居然写了类,因为会有一个投资组合页面,其中将包含有关客户的不仅仅是上述信息的详细信息。我知道一个班级只是在横幅中列出图片是有点矫枉过正的。
Aaahhh,dankie Bernhard! – 2010-08-05 13:34:31
可以通过数组跟踪多个Client对象吗? – 2010-08-05 13:41:00