我正在構建一個小的MVC系統(學習),我在查看文件中顯示變量時遇到了一些問題。__toString問題
這是從我的視圖類:
private $vars = array();
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
public function __get($key)
{
return $this->vars[$key];
}
public function __toString()
{
return $this->vars[$key];
}
public function show($file)
{
global $router;
$folder = strtolower($router->current_controller);
$path = VIEWPATH.$folder.'/'.$file.'.phtml';
if (! file_exists($path))
{
die("Template: $file, not found");
}
include ($path);
}
這裏是我的控制器:
$test = new View();
$test->name = 'karl';
$test->show('name_view');
和視圖文件(name_view)
echo $name // doesn't work
echo $this->name // Works
我在做什麼錯誤?也許我很想做出全球化的東西?
THX /托比亞斯
編輯:我剛纔解壓在視圖類權瓦爾陣列之前,包括我的視圖文件,然後它的工作。謝謝所有幫助。
show()函數有什麼作用? –
它包含視圖filde – sandelius
您的問題不在於__toString()方法,而是將類變量作爲全局變量來訪問。 –