OO Logic/Organization Help

I’m working on an OO application, I wanted to take the time to make the objects as flexible as possible. I understand basic OO, but I’m stuck on a couple basic ideas.

Part of the app will deal with Tv Series/Seasons/Episodes. I am using an API to grab this information but its important that I be able to easily interchange APIs. My goal is to build an underlying Series/Seasons/Episodes logic and then be able to put API logic on top of this. This functionality is proving more difficult to develop than I anticipated. I’d really appreciate any advise you could offer to get me moving in the right direction.


<?php

class TvAPI{
 
	public $url = "http://url.com";
	private $raw_data;
	private $series;
 
	public function __construct(){
	
	}

	public function set_series_info($title){
		$this->raw_data = simplexml_load_file($this->url."/search.php?show=".$title);
		$this->series = new Series();
		$this->series->setTitle($this->getTitle());
		$this->series->setID($this->getID());
		return $this->series->getID();
	}

	public function set_series_episodes($id){
		$episodes = simplexml_load_file($this->url."/episode_list.php?sid=".$id);
		$seasons = $episodes->Episodelist->Season;
		$newEpisodes = false;
		
		foreach($seasons as $season){
			$this->series->newSeason($season['epnum']);
			foreach($season->episode as $episode){
				if(strtotime($episode->airdate) > strtotime("now")){
					$newEpisodes = true;
					$this->series->addEpisode($season['epnum'],$episode->epnum, $episode->airdate, $episode->title);
				}
			}
		}
		
		return $newEpisodes;
	}
	
	private function getTitle(){
		return $this->raw_data->show->name;
	}
	
	private function getID(){
		return $this->raw_data->show->showid;
	}
	
	public function getSeries(){
		return $this->series;
	}
}

class Series{

	private $title;
	private $series_id;
	private $seasons;
	
	public function __construct(){
		$this->seasons = Array();
	}
	
	public function newSeason($num){
		$this->season[$num] = new Season($num);
	}
	
	public function addEpisode($series_num, $episode_num, $air_date, $title){
		$this->season[$series_num]->addEpisode($episode_num, $air_date, $title);
	}
	
	public function setTitle($title){
		$this->title = $title;
	}
	
	public function setID($id){
		$this->series_id = $id;
	}
	
	public function getTitle(){
		return $this->title;
	}
	
	public function getID(){
		return $this->series_id;
	}
	
	public function getSeasons(){
		return $this->seasons;
	}
}

class Season{

	private $season_num;
	private $episodes;
	
	public function __construct($num){
		$this->season_num = $num;
		$this->episodes = Array();
	}
	
	public function addEpisode($episode_num, $air_date, $title){
		$this->episodes[intval($episode_num)] = new Episode($episode_num, $air_date, $title);
	}
	
	public function getEpisodes(){
		return $this->episodes;
	}
}

class Episode{

	private $episode_num;
	private $air_date;
	private $title;
	
	public function __construct($episode_num, $air_date, $title){
		$this->episode_num = $episode_num;
		$this->air_date = $air_date;
		$this->title = $title;
	}
	
	public function getTitle(){
		return $this->title;
	}
	
	public function getAirDate(){
		return $this->air_date;
	}

}

?>

I would then call the TvAPI class which will through a cascade call the remaining classes. In addition to improving the logic of this functionality I have another question.

Say I have code like this:


foreach($shows as $show){
				$id = $mytv->set_series_info($show->{'name'});
				$result = $mytv->set_series_episodes($id);
				if($result){
					$calender->addSeries($mytv->getSeries());
				}
			}
			$calender->display();

class Calender{

	private $series;

	public function __construct(){
		$this->series = Array();
	}
	
	public function addSeries($series){
		$this->series[] = $series;
	}
	
	private function sort(){
		$sorted = Array();
		foreach($this->series as $show){
			var_dump($show);
			$seasons = $show->getSeasons();
			foreach($seasons as $season){
				$episodes = $show->getEpisodes();
				var_dump($episodes);
				foreach($episodes as $episode){
					$sorted[$episodes->getAirDate()][] = $episodes->getTitle();
				}
			}
		}
		return $sorted;
	}
	
	public function display(){
		$sorted = $this->sort();
		foreach($sorted as $date => $shows){
			print "<h2>".$date."</h2>";
			print "<ul>";
			foreach($shows as $show){
				print "<li>".$show."</li>";
			}
			print "</ul>";
		}
	}

}

In this code I’m passing the series object from the classes above to another object. I end up with an array of series/season/episode objects. I thought I would be able to do something like $seasons = $show->getSeasons(); as seen in the calendar sort function. However, this function does not appear to be accessible after the objected is passed around. How should I interact with these objects after they are returned from the API function and passed to another object?

I know thats a lot of information/questions, but I really hope to improve my understanding of OO with this personal project. Let me know if I can clarify anything further.

Thanks!