[英]New to OOP PHP, need critique on first Geo RSS Class
I'm completely new to OOP PHP and currently reading "PHP Objects, Patterns and Practice". 我是OOP PHP的新手,目前正在阅读“PHP对象,模式和实践”。 I needed to develop something that will generate a GeoRSS feed. 我需要开发一些可以生成GeoRSS Feed的东西。 This is what I have (it works perfectly, I would just like some critique as to what I could do different / more efficiently / safer): 这就是我所拥有的(它完美地运作,我只想对我能做什么不同/更有效/更安全的一些批评):
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;
}
}
protected
if there isn't a compelling reason to make them public
or private
. 如果没有令人信服的理由将其public
或private
则应始终protected
财产。 protected $items
in the class body and a $items = ''
in getFeed
. 在使用它们之前声明或启动所有变量:您缺少类体中protected $items
和getFeed
的$items = ''
。 $this->items = array();
正确启动变量: $this->items = array();
in __construct
. 在__construct
。 Don't manage an own item_count
. 不要管理自己的item_count
。 Better rely on PHP's own array appending facilities: 更好地依赖PHP自己的数组附加功能:
$this->items[] = array( 'pubDate' => date("D, j MYH: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, );
Much nicer, isn't it? 好多了,不是吗?
Don't declare more variables then you need: 根据需要,不要声明更多变量:
foreach ($this->items as $item) { $items .= " <item>\\n"; foreach ($item as $element => $value) { $items .= " <$element>$value</$element>\\n"; } $items .= " </item>\\n"; }
Here you didn't need the array key. 在这里你不需要数组键。 So don't fetch it in the foreach
loop ;) Instead use $item
for the value, which is better then $item_element
. 所以不要在foreach
循环中获取它;)而是使用$item
作为值,这比$item_element
更好。
Here are some points: 以下是一些观点:
setItem
, it indicates you have another object in play. 每当你看到你正在创建一个像在setItem
那样的大关联数组时,它表示你有另一个对象在起作用。 So make a class RSSItem
or something like that, with those things as members. 所以创建一个class RSSItem
或类似的东西,将这些东西作为成员。 If I think of more I'll edit my answer. 如果我想到更多,我会编辑我的答案。
The only qualm I have with this class is in your setItem
function, you should just use []
notation to push an associative array like this: 我在这个类中setItem
的唯一问题是在你的setItem
函数中,你应该只使用[]
表示法来推送一个关联数组,如下所示:
public function setItem ($item_pubDate, $item_title, $item_link, $item_description, $item_geolat, $item_geolong) {
$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);
}
This way you can ditch your $item_count
index variable. 这样你可以抛弃你的$item_count
索引变量。
Also, letting your properties be public
is usually a Very Bad Idea. 此外,让您的财产public
通常是一个非常糟糕的主意。
Looks good for a first timer! 第一个计时器看起来不错! Where do you process the data that you send as parameters? 您在哪里处理作为参数发送的数据? Personally, I would process everything withing the class methods, the purpose of objects is to contain, well, objects. 就个人而言,我会使用类方法处理所有内容,对象的目的是包含对象。 That means that all the processing related to them should happen inside the class itself. 这意味着与它们相关的所有处理都应该在类本身内部进行。
Also, maybe it's a good ideea to play with inheritance and public, private members, classes that use other classes, like Tesserex suggested. 此外,也许使用继承和公共,私人成员,使用其他类的类,如Tesserex建议,这是一个很好的想法。 Still, nice start on OOP there. 在那里,OOP仍然是一个不错的开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.