PHP模板类不起作用

吉普克

嗨,我的php模板类有问题。问题是当我打开index.php文件时没有显示任何内容。我不确定,但我认为问题出在模板类
template.php:

<?php
class Template {
    protected $template;
    protected $vars = array();
    public function __construct($template){
        $this->template = $template;
    }
    public function __get($key){
        return $this->vars[$key];
    }
public function __set($key, $value){
    $this->vars[$key] = $value;
}
public function __toString(){
    extract($this->vars);
    chdir(dirname($this->template));
    ob_start();
    include basename($this->template);
    return ob_get_clean();
}
}
?>

头版

<?php include('includes/header.php'); ?>
test
<?php include('includes/footer.php'); ?>

index.php:

<?php 
require 'ini.php'; 
$template = new Template('templates/frontpage.php');
echo $template;
?>

this.php:

<?php
session_start();
function __autoload ($class_name){
    require_once('libraries/'.$class_name'.php');
}
?>

注意:frontpage.php可以正常工作。

二湾

我已将您的代码放在本地Web服务器中,并检查了apache错误日志(这是您应该执行的操作),这是错误:

PHP解析错误:语法错误,第4行/var/www/html/template/ini.php中出现意外的“ .php”(T_CONSTANT_ENCAPSED_STRING)

问题是:

require_once('libraries/'.$class_name '.php');

代替

require_once('libraries/'.$class_name . '.php');

当我更正它时,还有另一个错误:

PHP致命错误:require_once():无法在第4行的/var/www/html/template/ini.php中打开所需的'libraries / Template.php'(include_path ='。:/ usr / share / php')

因此,看来您的libs目录中的所有类的文件名首字母都应大写。

改名

library / template.php

library / Template.php

注意:应将函数“ require”替换为“ require_once”,以避免出现多乘包含问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章