I just wrote my first ever class in PHPClasses are part of the idea of Object Based Programming (OBP). It just means that you can easily do something as many times as you need to - it is especially easy if you need to run functions again and again with the same parameters.
The class that I wrote is a simple RSS reader. You just have to create the object, and then set the URL of the feed, and then run two functions. It is only compatible with PHP 5 and upwards - if you don't know if you fit this requirement make sure you run "$RSSReader->CheckVersion();". Here is the main coding, RSSReader.class.php:
I expect, unless you have used classes before, which you likely haven't, you are confused now. You're probably asking how you use this! Well, here is my test.php:PHP Code:<?php
// RSS Reader in PHP5
// by Tim Rogers
class RSSReader {
var $FeedURL;
function CheckVersion() {
$yourphpversion = phpversion();
$comparison = version_compare($yourphpversion, '5.0.0');
if ($comparison == -1) {
die("To use RSSReader, you need to upgrade to at least PHP5.");
}
}
function EchoChannel() {
$_loadfeed = simplexml_load_file($this->FeedURL);
$channel_to_echo .= '<strong><u><a href="';
$channel_to_echo .= $_loadfeed->channel->link;
$channel_to_echo .= '">';
$channel_to_echo .= $_loadfeed->channel->title;
$channel_to_echo .= '</a></u></strong>';
echo $channel_to_echo;
}
function EchoArticles() {
$_loadfeed = simplexml_load_file($this->FeedURL);
foreach ($_loadfeed->channel->item as $item) {
echo "<a href='" . $item->link . "'>" . $item->title . "</a>";
echo "<br />";
echo $item->description;
echo "<br /><br />";
}
}
}
?>
If you want to see it in action, visit http://php5.tim-rogers.co.uk/RSSReader/test.php5PHP Code:<?php
// RSS Reader Class Test
// by Tim Rogers
// The script will fail unless the class can be loaded
require("RSSReader.class.php");
// Creates an "RSSReader" object
$RSSReader = new RSSReader;
// Checks your PHP version (not necessary)
$RSSReader->CheckVersion();
// Sets the URL of the feed you'd like to parse
$RSSReader->FeedURL = "http://www.habbo.co.uk/news/rss.xml";
// Sends the feed title to the browser, linked to the homepage of the feed
$RSSReader->EchoChannel();
echo "<br><br>";
// Shows the articles, linked to their full story, with a short description
$RSSReader->EchoArticles();
?>





Classes are part of the idea of Object Based Programming (OBP). It just means that you can easily do something as many times as you need to - it is especially easy if you need to run functions again and again with the same parameters.
Reply With Quote


It's Object Orientated Programming isn't it 