php模板类不起作用
您好,我的php模板类有问题。问题是当我打开index.php文件时,它没有显示任何内容。我不确定,但我认为问题在于模板类php模板类不起作用,php,Php,您好,我的php模板类有问题。问题是当我打开index.php文件时,它没有显示任何内容。我不确定,但我认为问题在于模板类 template.php: <?php class Template { protected $template; protected $vars = array(); public function __construct($template){ $this->template = $template; }
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();
}
}
?>
frontpage:
<?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;
?>
ini.php:
<?php
session_start();
function __autoload ($class_name){
require_once('libraries/'.$class_name'.php');
}
?>
注意:frontpage.php工作正常。我已将您的代码放入本地web服务器,并检查了apache错误日志(这是您应该做的),下面是错误: PHP分析错误:语法错误,意外“”。PHP“” (T_CONSTANT_ENCAPSED_STRING)在/var/www/html/template/ini.php的第行 四, 问题是:
require_once('libraries/'.$class_name '.php');
而不是
require_once('libraries/'.$class_name . '.php');
当我更正它时,出现了另一个错误:
PHP致命错误:require_once():无法打开所需文件
中的“libraries/Template.php”(include_path=”。:/usr/share/php)
/第4行的var/www/html/template/ini.php
因此,看起来库目录中的所有类都应该有其文件名的第一个字母upercase
改名
libraries/template.php
与
libraries/Template.php
注意:您应该将函数“require”替换为“require\u once”,以避免多重包含问题。您的页面上是否有可见的错误?如果没有,请确保有错误报告:
error\u reporting(E\u ALL)代码>。