代码之家  ›  专栏  ›  技术社区  ›  Anriëtte Myburgh

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

  •  0
  • Anriëtte Myburgh  · 技术社区  · 14 年前

    我想在我们的网站上列出我的投资组合,我写了一个课程,客户设置和获取每个客户的基本参数,即:客户的名字,网站,它的特色,它发布,我们为他们做了什么项目,等等。

    它相当原始,但是我是否正确地假设为每个客户机创建一个对象(并保持每个客户机的数据在一起),而不是为每个参数创建数组并每次从这些数组调用相应的元素?

    这是我的类,我知道它仍然是原始的,但是我也可以创建一些其他函数,所以现在我只是使用一个基本的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>
    

    任何帮助或建议都将不胜感激。事先谢谢。

    //编辑

    我忘记提了,我实际上写了这个类,因为会有一个投资组合页面,它将包含更多关于客户的信息,而不仅仅是上面提到的信息。我知道仅仅在横幅上列出图片有点过分了。

    2 回复  |  直到 12 年前
        1
  •  0
  •   Bernhard Häussermann    14 年前

    我对Web开发没有任何经验,但是在这里使用一个类绝对是更好的编程实践。
    管理多个并行阵列很麻烦。例如,每当插入新项或需要移动项时,都需要记住每个数组。此外,在这里使用类可以使代码更具组织性,从而更易于维护。
    我认为实现类的开销是值得的,尤其是当您掌握了在PHP中使用类的窍门之后!

        2
  •  0
  •   Community CDub    7 年前

    如果类是有效的解决方案,则取决于如何实现它。这不像你的代码会神奇地得到增强,因为你在某个东西上加了一个类声明。

    使用类的一个关键思想是封装状态和责任。所以要上课 Client 封装与客户机相关的状态是一个好主意。使其方法将自身呈现为 List Table 不是。我知道,您没有这样做,但是您也没有使用一个类,该类有责任将客户机呈现到列表或表中。也没有类来查找相关的客户机文件。第二个发布的代码块是过程脚本。它不使用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 .