Hi all, hopefully my question is simple as I'm fairly new to OOP coding. I've found an RSS Parser on the internet (which is open source and I can adapt it to however I please yada yada). I want to hide certain variables.

<?php
#		RSSParser
##		Using the SimpleXmlElement extention built into PHP5, take any RSS feed and parse the contents.

class RSSParser
{

	# (string) - URL of feed.
	var $url;
	# (array) - RSS Feed Information (title, desc, publish date).
	var $info;
	# (object) - SimpleXML Object data of RSS Feed.
	var $xml;
	# (object) - Channel Object containing all feed information and items.
	var $channel;
	# (array) - RSS Items.
	var $items;


	/*
        Class Constructor
			- This function is run everytime a class is instantiated.

		Arguements:
			url: Feed URL.
	*/
	function __construct($url)
	{
		# If the version of PHP is less than version 5, then die.
		if (intval(phpversion()) < 5)
		{
			die("PHP5 is required to execute this class.");
		}
		# Else if the extension class doesn't exist.
		else if (!class_exists("SimpleXMLElement"))
		{
			die("Please re-compile PHP5 with the simpleXmlElement extention.");
		}

		# Get the page contents of that feed.
		$this->getRSS($url);

		# Parse RSS information.
		$this->parseRSS();
	}


	/*
		Function getRSS
			- Get the feed source of the rss feed.

		Arguments:
			url - string of the url to the feed.
	*/
	function getRSS($url)
	{
		# Sets the internal url variable (propertie) to the passed in URL.
		$this->url = $url;
		
		# Sets the internal feed variable (propertie) to the contents of the URL specified earlier, or else die if unsuccesssful.
		$this->info = file_get_contents($this->url) or die ("RSS feed was not found.");
	}


	/*
		Function: parseRSS
		- Parses the rss source.
		- Places feed items in array: $this->items.
		- Places feed details in array: $this->feed.

		Arguments:
			None.
	*/
	function parseRSS()
	{
		# Create a new instance of this class.
		$this->xml = new SimpleXMLElement($this->info);
        
		# The XML Object has another child called channel which holds the RSS details as well as items.
		$this->channel = $this->xml->channel;

		# This variable is an array of the RSS feed information.
		$this->info = array
		(
			"title" => $this->clean($this->channel->title),
			"description" => $this->clean ($this->channel->description),
			"link" => $this->clean ($this->channel->link),
			"copyright" => $this->clean ($this->channel->copyright),
			"lastBuildDate" => $this->clean($this->channel->lastBuildDate),
			"image" => ($this->channel->image->url) ? $this->clean($this->channel->image->url) : false
		);

		# Checks if there are any items present.
		if (is_object($this->channel->item) && count($this->channel->item))
		{
			# Loop through all the <item> objects.
			foreach($this->channel->item as $item)
			{
				# Add an item to the array
				$this->items[] = array(
					"title" => $this->clean($item->title),
					"description" => $this->clean ($item->description),
					"link" => $this->clean ($item->link),
					"pubDate" => $this->clean($item->pubDate)
				);
			}
		}
	}


	/*
		Function: clean
			- Cleans off the object tag from an object variable.

		Argueuemts:
			i - string in which to clean.
	*/
	function clean ($i)
	{
		return (string) htmlspecialchars(html_entity_decode($i));
	}

}
?>

The only variables I want coming back are info and items, but if i print out the object, i get every one of those variables coming back. I tried adding Private instead of var but that didn't work. Can anyone guide me?

Thanks!

Anthony

Misconception of OOP.

Anthony>but if i print out the object?

using print_r or var_dump? - They are for debug purpose.

Private members of class are not accessible/manipulated directly outside the class.

Like adatapost said you contain a few misconceptions of PHP OOP.
- Setting the variables access to private, protected or public will not alter it from being printed rather it being manipulated by outside classes.
- When you say you want to display the object you refer to use of the variable? displaying the variable?
- If you literally just want to print out a single value of the array why not just create a function that refers to the "item->name" and prints it out the required value?

Let us know how you go.

Thanks to all for replying, I set the variables to private and although I can debug them i.e. print_r, and see them, I cannot change them, which I guess is the meaning of them being private!

Thanks once again guys,

Anthony

This article has been dead for over six months. Start a new discussion instead.