I'm rewriting a lot of my old mucky spaghetti code and am currently trying to get to grips with classes, functions and loosely rebuilding my site around MVC model.
However, I cannot get my header template include, to reference user account details which are autoloaded by my main config file and I think I'm missing out a very important step.
The error message is Fatal error: call to member function is_loggedin() on a non-object...
, so I'm guessing that the include performed in template.class.php cannot access the account.class.php functions.
UPDATE: peforming a var_dump(get_included_files())
within header.tpl.php shows that both account.class.php and template.class.php are included (in that order). I also tried manually including account.class.php at the top of header.tpl.php to see if it would make any difference...it didnt. HELP :(
I should also note that I can call $_account->is_loggedin()
from index.php with no issues. Jst not from within the included file header.inc.php.
Chances are I'm going abut this all wrong, so below is a simplified write-up of my code if anyone can offer some pointers:
index.php
<php require 'defaults.php'; ?>
<html>
<head>
...
</head>
<body>
<?php $_template->load('header'); ?>
....
</body>
defaults.php
session_start();
// define path settings
// connect to database, memcache
// lots of other stuff....
//autoloader for functions
spl_autoload_register(function ($class) {
if (file_exists(CLASS_PATH.DS.$class.'.class.php'))
{
include CLASS_PATH.DS.$class.'.class.php';
}
});
$_account = new account(); // autoload user account stuff
$_template = new template(); // autoload templates
account.class.php
class account
{
private $db;
public function __construct($db) {
$this->db = $db;
}
public function is_loggedin() {
// do various session checks
}
}
template.class.php
class template
{
public function load($template)
{
if (file_exists(TPL_PATH.DS.$template.'.tpl.php'))
{
include TPL_PATH.DS.$template.'.tpl.php';
}
}
}
header.tpl.php
<div id="header">
<?php if($_account->is_loggedin() == true): ?>
<p>logged in</p>
<?php else: ?>
<p>not logged in</p>
<?php endif; ?>