PHP类是列出内容的有效解决方案吗?

时间:2010-08-05 09:08:51

标签: php html class text

我想在我们的网站上列出我的投资组合,并且我写了一个类,客户端用于设置和获取有关每个客户的基本知识的参数,即:客户的名称,网站,是否有特色,是否已发布,哪些项目已执行我们为他们做了等等。

这是相当原始的,但我认为最好为每个客户端创建一个对象(并将每个数据保持在一起),而不是为每个参数创建数组并每次从这些数组中调用相应的元素?

这是我的课程,我知道它仍然是原始的,但我也可以创建其他一些函数,所以现在我只是使用一个基本的getter和setter来获取或设置每个Client的参数。

<?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});
    }
}

?>

我仍在计划我可能想要添加到课程中的功能,但现在就是它。

这是我用来创建每个Client对象的其他代码:

<?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>

非常感谢任何帮助或建议。提前谢谢。

//编辑

我忘了提及,我实际上写了这个类,因为会有一个Portfolio页面,它将包含有关客户端的更多信息,而不仅仅是上面提到的信息。我知道,如果只是在横幅中列出图像,那么课程就有点过分了。

2 个答案:

答案 0 :(得分:0)

我没有任何网络开发经验,但在这里使用课程肯定是更好的编程实践。
管理多个并行阵列很麻烦。例如,无论何时插入新项目或需要移动项目,都需要记住每个数组。此外,在这里使用一个类可以使您的代码更有条理,因此更易于维护 我认为实现该类的开销肯定是值得的,特别是一旦你掌握了在PHP中使用类的想法!

答案 1 :(得分:0)

如果一个类是一个有效的解决方案取决于你如何实现它。这并不是说你的代码会神奇地被强化,因为你在某事上打了一个类声明。

使用类的一个关键思想是封装状态和责任。因此,拥有一个封装客户端相关状态的类Client是一个好主意。给它自己呈现为ListTable的方法不是。我知道,您没有这样做,但您也没有使用有责任将客户端呈现为列表或表的类。您也没有课程来查找相关的客户端文件。您的第二个发布的代码块是一个程序脚本。它不使用OOP。识别问题并将其分解为小型(可测试)单元。将它们合并到适当的类中。

有一个很好的series of articles by Lorna Jane Mitchell about PHP5 and OOP,还有a number of good Q&A on StackOverflow as well。你还想看看what Design Patterns are。并here is a shameless self-plug