为了更为容易地生成Feed,创建类"RSS 工厂"。在这里要第一次使用面向对象编程(OOP)。
前面一个示例避免了使用PHP的面向对象编程支持。在此之所以使用它,是因为它的确使得事情简单化。它的用法半在示例中有所讲解。
(一)在seophp/include文件夹创建新文件rss_factory.inc.php。在这个文件中将创建RSSFactory类。在rss_factory.inc.php文件中键入如下代码:
<?php
class RSSFactory
{
var $_title;
var $_link;
var $_description;
var $_language;
var $_items;
function _escapeXML($str)
{
$translation=get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
foreach($translation as $key=>$value){
$translation[$key]='&#'.ord($key).';';
}
$translation[chr(38)]='&';
return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&",strtr($str,$translation));
}
function RSSFactory($title,$link,$dascription,$language='en-us',$items=array()){
$this->_title=$title;
$this->_link=$link;
$this->_description=$description;
$this->_language=$language;
$this->_items=$items;
}
function addItem($title,$link,$description,$additional_fields=array()){
$this->_items[]=array_merge(array('title'=>$title,'link'=>$link,'description'=>$description),$additional_fields);
}
function get(){
ob_start();
header('Content-type:text/xml');
echo '<rss version="2.0">'.
'<channel>'.
'<title>'.RSSFactory::_escapeXML($this->_title).
'</title>'.
'<link>'.RSSFactory::_escapeXML($this->_link).'</link>'.
'<description>'.RSSFactory::_escapeXML($this->_description).'</description>';
foreach($this->_items as $feed_item){
echo '<item>';
foreach($feed_item as $item_name=>$item_value){
echo "<$item_name>".RSSFactory::_escapeXML($item_value)."</$item_name>";
}
echo "</item>";
}
echo '</channel></rss>';
return ob_get_clean();
}
}
?>
(二)在seophp文件夹创建新文件feed.php,然后键入如下代码:
<?php
require_once 'include/rss_factory.inc.php';
$rss_feed=new RSSFactory('SEOEgghead.com New Products Feed','http://www.seoegghead.com/seo-with-php-updates.html','Exciting new products,updated daily');
$rss_feed->addItem('New Link Juice with Orange Flavor!','http://seophp.example.com/Products/SEO-Toolbox-C6/Link-Juice-P31.html','The new Link Juice Product of SEOEgghead.com can do wonders for your website!');
$rss_feed->addItem('Enhance Your PHP Applications with AJAX!','http://seophp.example.com/Products/Friends-Shed-C2/AJAX-PHP-Book-P42.html','Check out this AJAX tutorial for PHP developers!');
echo $rss_feed->get();
?>
(三)加载http://seophp.example.com/feed.php。现代WEB浏览器将会询问是否希望订阅该Feed。
(四)单击“Subscribe to this feed”链接将会显示一个对话框,从中可以选择订阅选项。
(五)在订阅该Feed之后,就可以通过特定的Feed阅读器应用程序来快速访问最新条目。
刚才已经创建了RSSFactory类,下面使用RSSFactory对象来生成RSS格式的简单Feed。
这个类的使用非常简单——只要引用rss_factory.inc.php文件,然后创建$rss_feed对象:
<?php
require_once 'include/rss_factory.inc.php';
$rss_feed=new RSSFactory('SEOEgghead.com New Products Feed','http://www.seoegghead.com/seo-with-php-updates.html','Exciting new products,updated daily');
这个类名为RSSFactory,而创建的对象名为$rss_feed。正如代码中所见,使用new 操作符来创建对象,并在类名后面的圆括号中放入参数。
在创建对象时提供的这些参数,将在对象创建过程中传递给类的构造函数。构造函数是类中的一个特殊方法,它在对象创建时自动调用。使用它就可以根据参数进入一些设置。在这个示例中,当创建RSSFactory对象时,传递了Feed的标题、链接和描述。这些都是和Feed本身相关的属性,而不是单个的单元。RSSFactory类中的构造函数定义如下所示:
function RSSFactory($title,$link,$description,$language='en-us',$items=array()){
$this->_title=$title;
$this->_link=$link;
$this->_description=$description;
$this->_language=$language;
$this->_items=$items;
}
于是构造函数将参数值保存到它的属性中,设置了对象的状态。请注意$this的用法,当在类中使用时,它意味着“当前的类实例”。
回到feed.php,在$rss_feed对象创建之后,两次调用addItem方法来添加两个Feed单元: