Accessing functions from an extending class
Posted: Sun Apr 26, 2009 5:19 pm
Hi all,
I've been playing with OOP for a bit now and I'm liking the way it's working so far (it's a lot more condensed!), but at the moment I have one 'main' class which has all of the functions. Problem is, this file is included in every page and some of the functions are only relevant to very few pages, so I moved the barely used ones into a file to be included. This is where I discovered you couldn't add a function to a class by including it directly - is this where you need a new class to extend the first?
Here's a snippet of init.class.php (which calls rss.inc.php):
So I have a file which initializes the session included in every viewable page, giving me access to the class through $app. I assumed if I ran $app->get_rss() then $rss would be available in the same way, e.g. $rss->rss_display('www.someplace.com/rss.xml') - but it's not! I tried using $app->rss_display, init::rss_display, rss::rss_display and it either says the class/function/variable isn't defined! I'm sure there is an easy solution but everything I read about OOP just confuses the matter 
Ideally I would really like to be able to extend the class init so all the functions of rss_parse become available through $app, is that possible? Any help would really be appreciated!
Thanks, Ben
P.S. here's a little snippet of rss.class.php:
I've been playing with OOP for a bit now and I'm liking the way it's working so far (it's a lot more condensed!), but at the moment I have one 'main' class which has all of the functions. Problem is, this file is included in every page and some of the functions are only relevant to very few pages, so I moved the barely used ones into a file to be included. This is where I discovered you couldn't add a function to a class by including it directly - is this where you need a new class to extend the first?
Here's a snippet of init.class.php (which calls rss.inc.php):
Code: Select all
if (!defined('ACTIVE')) {header("Location: ../login.php"); exit;}
class init {
private $testmode,$dbname;
...
public function __construct() {
...
}
public function get_rss() {
if (file_exists($this->libpath.'rss.inc.php')) {
require_once($this->libpath.'rss.inc.php');
$rss = new rss_parse;
return true;
}
return false;
}
# many more functions ...
}Ideally I would really like to be able to extend the class init so all the functions of rss_parse become available through $app, is that possible? Any help would really be appreciated!
Thanks, Ben
P.S. here's a little snippet of rss.class.php:
Code: Select all
...
class rss_parse extends init {
public function rss_parse() {
# do nothing
}
# the rss parsing functions
...
}