Page 1 of 1

Accessing functions from an extending class

Posted: Sun Apr 26, 2009 5:19 pm
by ben.artiss
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):

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 ...
}
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 :lol:

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
    ...
}

Re: Accessing functions from an extending class

Posted: Sun Apr 26, 2009 5:56 pm
by nyoka
Have you tried initiating your rss class within the main class like:

Code: Select all

 
$this->rss = new rss_parse();
 
Then you should be able to call the functions in the rss class like:

Code: Select all

 
$app->rss->rss_display();
 

Re: Accessing functions from an extending class

Posted: Sun Apr 26, 2009 6:03 pm
by ben.artiss
Awesom I just tried:

Code: Select all

$this->rss = new rss_parse();
...
$app->rss_display();
- which didn't work! Then I read what you said and the extra ->rss made all the difference! Thanks for that, I was about to shout at the screen! :P