2016-06-27 49 views
-1

嗨我的問題是我的PHP模板class.the問題是當我打開index.php文件它不顯示任何東西。我不知道,但我認爲問題是與模板類
的template.php: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; 
?> 

ini.php:

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

注意:在frontpage.php正常工作。

+1

您的網頁上是否有任何錯誤可見?如果沒有,請確保報告錯誤:'error_reporting(E_ALL);'。 – Jer

回答

2

我已經把你的代碼在我的本地Web服務器和Apache的錯誤日誌檢查(這是你應該所做的),這裏是錯誤:

PHP Parse error: syntax error, unexpected ''.php'' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/template/ini.php on line 4

的問題是:

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

代替

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

當我糾正之後,有另一個錯誤:

PHP Fatal error: require_once(): Failed opening required 'libraries/Template.php' (include_path='.:/usr/share/php') in /var/www/html/template/ini.php on line 4

因此,它看起來像庫目錄中的所有類應具有文件名稱upercase的第一個字母。

重命名

庫/ template.php文件

庫/ template.php文件

注意:此時應更換功能 「需要」 與 「require_once」,以避免multipe包容的問題。