Page 1 of 1

Call to a member function on a non-object

Posted: Fri Jul 24, 2009 10:57 am
by bluefusionstudios
I'm a bit of a newbie to object oriented PHP. There are three classes: db.class.php (for database functions), page.class.php (for templating and page display), and session.class.php (for logging in and out).

index.php includes the required template stored in the templates folder and starts the required objects.

But i keep getting this error whenever I call to a function in page.class.php
Fatal error: Call to a member function get_website_stats() on a non-object in C:\xampp\htdocs\kingsdale\admin\templates\dashboard.php on line 9
index.php (minus the html)

Code: Select all

<?php
// required files
require_once("classes/db.class.php");
require_once("classes/page.class.php");
require_once("classes/session.class.php");
 
// new db object
$db = new db;
// check database connection
if (!$db->connect()) 
   $db->print_last_error(false);
   
// new page and templating objects
$page = new page;
 
// new session object
$session = new session;
 
// html head below
?>
 
<?php 
// check if user is logged in
if(!$session->loggedIn && $template['protected']) {
    include("templates/login.php");
} else {
?>
// html body
<?
    $page->load_template();
}
?>
templates/dashboard.php

Code: Select all

<div class="med">
        
    <div class="head">  
        <div class="text">Website Statistics</div>
        <div class="clear"></div>           
    </div>
                    
    <div class="boxcontent">            
        <?=$page->get_website_stats();?>          
    </div> 
                    
    <div class="wht"></div>    
</div>
page.class.php

Code: Select all

<?php
 
class page {
    
    function load_template() {
        if($template['tpl']) {
            // include page template
            $tpl_path = "templates/".$template['tpl'].".php";
            
            // check that template exists   
            if(file_exists($tpl_path)) {
                include($tpl_path);
            } else {
                echo "Cannot find template, ".$tpl_path;
            }
        } else {
            // load dashboard as default
            include("templates/dashboard.php");
        }
    }
    
    function get_website_stats() {
        $r = $this->db->select("SELECT * FROM modules WHERE active = '1' ORDER BY rank");
        
        while ($row=$this->db->get_row($r, 'MYSQL_ASSOC')) {
            include('modules/'.$row['path'].'/stats.php');
        }
    }
 
}
 
?>

Re: Call to a member function on a non-object

Posted: Fri Jul 24, 2009 11:02 am
by Darhazer
You are including the template file, but in the function, that includes the file, there is no variable, called $page, and it have no access to the global variables...

Re: Call to a member function on a non-object

Posted: Fri Jul 24, 2009 11:21 am
by bluefusionstudios
I initialised a db variable and added a class constructor then changed $page = new page to $page = new page($db) and it all works

Thanks alot :mrgreen: