php - OOP PHP 新手,需要对第一个 Geo RSS 类进行评论

标签 php oop class rss georss

我是 OOP PHP 的新手,目前正在阅读“PHP 对象、模式和实践”。我需要开发一些可以生成 GeoRSS 提要的东西。这就是我所拥有的(它工作得很好,我只是想对我可以做的不同/更有效/更安全的事情提出一些批评):

class RSS {
 public $channel_title;
 public $channel_description;
 public $channel_link;
 public $channel_copyright;
 public $channel_lang;
 public $item_count;
 public function __construct ($channel_title, $channel_description, $channel_link, $channel_copyright, $channel_lang) {
  $this->channel_title = $channel_title;
  $this->channel_description = $channel_description;
  $this->channel_link = $channel_link;
  $this->channel_copyright = $channel_copyright;
  $this->channel_lang = $channel_lang;
  $this->items = "";
  $this->item_count = 0;
    }
 public function setItem ($item_pubDate, $item_title, $item_link, $item_description, $item_geolat, $item_geolong) {
  $this->items[$this->item_count]['pubDate'] = date("D, j M Y H:i:s T",$item_pubDate);
  $this->items[$this->item_count]['title'] = $item_title;
  $this->items[$this->item_count]['link'] = $item_link;
  $this->items[$this->item_count]['description'] = $item_description;
  $this->items[$this->item_count]['geo:lat'] = $item_geolat;
  $this->items[$this->item_count]['geo:long'] = $item_geolong;
  $this->items[$this->item_count]['georss:point'] = $item_geolat." ".$item_geolong;
  $this->item_count++;
 }
 public function getFeed () {
  foreach ($this->items as $item => $item_element) {
   $items .= "    <item>\n";
   foreach ($item_element as $element => $value) {
    $items .= "      <$element>$value</$element>\n";
   }
   $items .= "    </item>\n";
  }
  $feed = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    . "<rss version=\"2.0\" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\" xmlns:georss=\"http://www.georss.org/georss\">\n"
    . "  <channel>\n"
    . "    <title>".$this->channel_title."</title>\n"
    . "    <description>".$this->channel_description."</description>\n"
    . "    <link>".$this->channel_link."</link>\n"
    . "    <copyright>Copyright ".date("Y")." ".$this->channel_copyright.". All rights reserved.</copyright>\n"
    . "    <lang>".$this->channel_lang."</lang>\n"
    . $items
    . "  </channel>\n"
    . "</rss>";
  return $feed;
 }
}

最佳答案

  1. 如果没有令人信服的理由将属性设置为publicprivate,则属性应始终受到protected
  2. 在使用它们之前声明或启动所有变量:您在类主体中缺少一个 protected $items 并且在 getFeed 中缺少一个 $items = ''
  3. 正确初始化变量:$this->items = array(); in __construct
  4. 不要管理自己的item_count。更好地依赖 PHP 自己的数组附加工具:

    $this->items[] = array(
        'pubDate'      => date("D, j M Y H:i:s T",$item_pubDate),
        'title'        => $item_title,
        'link'         => $item_link,
        'description'  => $item_description,
        'geo:lat'      => $item_geolat,
        'geo:long'     => $item_geolong,
        'georss:point' => $item_geolat." ".$item_geolong,
    );
    

    好多了,不是吗?

  5. 不要声明你需要的变量:

    foreach ($this->items as $item) {
        $items .= "    <item>\n";
        foreach ($item as $element => $value) {
             $items .= "      <$element>$value</$element>\n";
        }
        $items .= "    </item>\n";
    }
    

    在这里你不需要数组键。所以不要在 foreach 循环中获取它;)而是使用 $item 作为值,这比 $item_element 更好。

关于php - OOP PHP 新手,需要对第一个 Geo RSS 类进行评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3999691/

相关文章:

php - 需要 Multi-Tenancy 数据库架构的建议?

php - 计算菜单项返回的行数

c++ - C++中的继承语法

PHP - 字符串 - 删除具有特定类的 HTML 标记,包括其内容

python - 将装饰器函数合并为类

php - 使用javascript获取表单中最接近的元素

java - 如何将内部函数变成自己的函数?

.net - Ninject, "Abstract Factory"模式和运行时条件解析

c++ - 为什么要使用 const 成员函数?

php - 查看未找到 Laravel