2010-09-27 93 views
2

這些都是類定義簡單的手工PHP模板引擎不工作!請幫助

<?php 
    abstract class MyTemplate { 

    protected $arrayOfSpaces; 
    protected $arrayOfVariables; 
    protected $output; 

    protected abstract function __construct(); 

    function outputHTML(){ 
    echo $output; //Apparently, the problem is HERE. <<<<>>>>> 
    } 
} 
    class MyTemplateMain extends MyTemplate { 
    function __construct(){ 
    $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" 
      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
      <html> 
      <head> 
      </head> 
      <body> 
      I love Rock n Roll!!! 
      </body> 
      </html>"; 
    } 

    } 
?> 

而這正是我推出這個網頁

<?php 
    require_once("view/templates.php"); 

    $page=new MyTemplateMain(); 
    $page->outputHTML(); 



?> 

不工作,雖然。只是顯示一個空白頁面,沒有字符串我愛搖滾本應出現在身體。

我敢肯定有實施模板更好的辦法,但我只是想弄清楚爲什麼這個特殊的例子不工作

任何幫助表示讚賞。由於

PS:引號都逃脫正式和文件路徑也沒關係

+0

'這$ output'應該是'$這個 - > output' – Jacco 2010-09-27 09:00:42

+0

這裏打字時我拼錯了。 ..我正在運行的腳本是可以的。 – 2010-09-27 09:08:25

回答

2

變化:

function outputHTML(){ 
    echo $output; 
    } 

到:

function outputHTML(){ 
    echo $this->output; 
    } 
+0

嗯......如果我不使用$ this,PHP無法猜測我在說哪個變量?或者這只是一種風格? – 2010-09-27 12:28:55

+1

PHP甚至不會猜測。總是需要'$ this'。 – 2010-09-27 12:32:54

+0

php不會...猜對 – Hannes 2010-09-27 12:49:37

2

你的語法很奇怪,試試這個

abstract class MyTemplate { 

    protected $arrayOfSpaces; 
    protected $arrayOfVariables; 
    protected $output; 

    public abstract function __construct(); 

    function outputHTML(){ 
    echo $this->output; 
    } 
} 
    class MyTemplateMain extends MyTemplate { 
    public function __construct(){ 
    $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" 
      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
      <html> 
      <head> 
      </head> 
      <body> 
      I love Rock n Roll!!! 
      </body> 
      </html>"; 
    } 

    } 

$page=new MyTemplateMain(); 
$page->outputHTML(); 
+0

嗯,你的意思是在一個文件中的一切?這不應該是問題的根源,但好吧。做到了。 STill不工作..我想我一定忽略了一些細節..Thx無論如何 – 2010-09-27 08:52:39

+1

沒有抱歉:)你讓我錯了,文件不是問題(但我在一個節省時間) 。首先,我不知道任何OOP角度PHP5(或5.3),但我主要改變了訪問變量的方式($ this-> output只輸出$或output。$ output)並將__constructur更改爲public – Hannes 2010-09-27 09:25:57

+0

我想我解決了它。我想你必須命名爲ABSTRACT PROTECTED而不是PROTECTED ABSTRACT。顯然,「抽象」關鍵字首先是 – 2010-09-27 09:37:41

0

嘗試$base->output代替$this->output

0

好的。我不知道爲什麼,但現在它工作。我重寫了一段時間,所以即使意外,我也一定做了一些正確的事情。這裏是代碼,如果任何人想要熟悉並比較版本。感謝所有

abstract class MyTemplate { 

protected $arrayOfSpaces; 
protected $arrayOfVariables; 
protected $output; 


function outputHTML(){ 
    echo $this->output; 
} 

} 

class MyTemplateMain extends MyTemplate { 

function __construct(){ 
    $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" 
      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
      <html> 
      <head> 
      </head> 
      <body> 
      I love Rock n Roll!!! 
      </body> 
      </html>"; 
} 
} 
+1

如果outputHTML函數中的第一個示例'echo $ output;'回顯未定義的變量,然後你有'echo $ this-> output;'輸出對象字段。 – jcubic 2010-09-27 10:20:16

+1

問題不在於抽象和保護的順序,它在類上下文中對變量的處理 – Hannes 2010-09-27 10:44:34