Accessing functions from an extending class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Accessing functions from an extending class

Post 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
    ...
}
nyoka
Forum Commoner
Posts: 45
Joined: Thu Apr 09, 2009 12:53 pm

Re: Accessing functions from an extending class

Post 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();
 
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Accessing functions from an extending class

Post 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
Post Reply