Call to a member function on a non-object

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
bluefusionstudios
Forum Newbie
Posts: 3
Joined: Fri Jul 24, 2009 10:16 am
Location: London

Call to a member function on a non-object

Post 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');
        }
    }
 
}
 
?>
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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...
bluefusionstudios
Forum Newbie
Posts: 3
Joined: Fri Jul 24, 2009 10:16 am
Location: London

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

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