OOP PHP的新手,需要对第一个Geo RSS Class进行批评
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了OOP PHP的新手,需要对第一个Geo RSS Class进行批评,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2929字,纯文字阅读大概需要5分钟。
内容图文
![OOP PHP的新手,需要对第一个Geo RSS Class进行批评](/upload/InfoBanner/zyjiaocheng/758/9241f4072d87451987d4200f666f3004.jpg)
我是OOP PHP的新手,目前正在阅读“PHP对象,模式和实践”.我需要开发一些可以生成GeoRSS Feed的东西.这就是我所拥有的(它完美地运作,我只想对我能做什么不同/更有效/更安全的一些批评):
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;
}
}
解决方法:
>如果没有令人信服的理由将属性公开或私有,则应始终保护属性.
>在使用之前声明或启动所有变量:您缺少类体中受保护的$项目和getFeed中的$items =”.
>正确启动变量:$this-> items = array();在__construct中.
>不管理自己的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,
);
好多了,不是吗?
>不要声明您需要的更多变量:
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更好.
内容总结
以上是互联网集市为您收集整理的OOP PHP的新手,需要对第一个Geo RSS Class进行批评全部内容,希望文章能够帮你解决OOP PHP的新手,需要对第一个Geo RSS Class进行批评所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。