注意:未定义的变量:template&;致命错误:在非对象上调用成员函数writePage()


Notice: Undefined variable: template & Fatal error: Call to a member function writePage() on a non-object

我有个问题。当我运行我的网站时,模板错误。

我得到这两个错误:

Notice: Undefined variable: template in /home/us22368/domains/******.nl/public_html/index.php on line 4 
Fatal error: Call to a member function writePage() on a non-object in /home/us22368/domains/******.nl/public_html/index.php on line 4

部分代码:

index.php:

<?php
require_once('global.php');
$template->writePage('index');
$template->echoPage();
?>

我的global.php包括这个类(class.template.php):

<?php
if (!class_exists('template')) {
    class template {
        public $template;
        public function writePage($pageName) {
            $file = 'app/themes/' . THEME . '/' . $pageName . '.html';
            if(!file_exists($file)) {
                die('The Page You Asked for does not exist.');
            } else {
            $this->content .= file_get_contents($file);
            }
        }
        public function echoPage() {
            echo $this->content;
        }
    }
}
?>

有人能帮我吗?

在index.php文件中,您必须创建一个模板类实例:

<?php
require_once('global.php');
$template = new template();
$template->writePage('index');
$template->echoPage();
?>

查看php文档:http://www.php.net/manual/en/language.types.object.php