JavaScript Editor Ajax software     Free javascripts 



Main Page

These parameters you provide when creating an object are passed to the class
constructor
upon object
creation. The constructor is a special method inside the class that gets called automatically when the
object is created. You use it to set up some things based on the parameters. In this case, when creating
an
RSSFactory
object, you pass the feed title, link, and description. These are the attributes that are
associated with the overall feed itself, not its individual elements. The constructor definition looks like
this, in the
RSSFactory
class:
// the class constructor is executed when creating an instance of the class
function RSSFactory($title, $link, $description,
$language = ‘en-us’, $items = array())
{
// save feed data to local class members
$this->_title = $title;
$this->_link = $link;
$this->_description = $description;
$this->_language = $language;
$this->_items = $items;
}
So the constructor saves the parameter values to its properties, setting the object’s state. Note the usage of
$this
, which means “the current class instance” when used inside a class. (If you create more objects of
type
RSSFactory
, the
$this
reference in each of those objects will be different.)
Back in
feed.php
, after the
$rss_feed
object is created, its
addItem
method is called twice to add two
feed items:
// create feed
$rss_feed = new RSSFactory(‘SEOEgghead.com New Products Feed’,
‘http://www.seoegghead.com/seo-with-php-updates.html’,
‘Exciting new products, updated daily’);
// add feed item
$rss_feed->addItem(‘New Link Juice with Orange Flavor!’,
‘http://localhost/seophp/Products/SEO-Toolbox-C6/Link-Juice-P31.html’,
‘The new Link Juice product of SEOEgghead.com can do wonders for your website!’);
// add feed item
$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!’);
Finally, the
get()
method is called to echo the RSS feed structure. The output of this particular exercise
will be as follows:
<rss version=”2.0”>
<channel>
<title>SEOEgghead.com New Products Feed</title>
<link>http://www.seoegghead.com/new-products.html</link>
<description>Exciting new products, updated daily</description>
<item>
<title>New Exciting Product</title>
159
Chapter 7: Web Feeds and Social Bookmarking
c07.qxd:c07 10:42 159


JavaScript Editor Ajax software     Free javascripts