2
我有這個功能的類:創建需要()別名功能
public function loadTemplate($template)
{
return require "templates/$template.php";
}
這很好,但我有一個問題的。
比方說,我有這樣的代碼:
require('class.php');
$class = new ClassName; // ClassName is the class which contains the function loadTemplate()
$name = 'Jerry';
$class->loadTemplate('myname');
而且「myname.php」是:
<?php
echo "My name is $name.";
在這種情況下,我得到一個錯誤,因爲「myname.php」實際上包含在課程文件中,因此$ name未定義。
我該如何解決這個問題?
老實說,我建議你不要這樣設計你的應用程序。或者,您需要將「變量」傳遞給loadTemplate方法一些方法,然後讓它們以某種方式傳遞給$模板。 – Daniel
你認爲什麼是更好的解決方案? –
「使用,分析和從現有的經過驗證的模板系統學習」將是一種選擇。 – VolkerK