主控制器.php:
final class MainController extends Controller {
public function is_locked() {
$lock = new View('lock');
Res::render($lock);
}
}
view.class.php:
final class View {
private $data;
public function get_title() {
return isset($this->title) ? $this->title : DEFAULT_TITLE;
}
public function get_layout() {
return isset($this->layout) ? $this->layout : 'base';
}
public function get_layout_path() {
return SITE_PATH .'app/views/layouts/'. $this->get_layout() .'.layout.php';
}
public function get_path() {
return SITE_PATH .'app/views/' .$this->name .'.view.php';
}
public function print_title() {
echo $this->get_title;
}
public function __construct($name) {
$this->data = array();
$this->data['name'] = $name;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error('Undefined property via __get(): '. $name .' in '. $trace[0]['file'] .' on line' . $trace[0]['line'], E_USER_NOTICE);
return null;
}
public function __isset($name) {
return isset($this->data[$name]);
}
public function __unset($name) {
unset($this->data[$name]);
}
}
这是我的响应类的渲染方法:
public static function render($view) {
include_once SITE_PATH .'app/views/layouts/root.layout.php');
}
从我的一个控制器调用...参数 $view 是一个简单的 View 对象...
这是我的 root.layout.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?php $view->print_title(); ?></title>
</head>
<body>
root layout
</body>
</html>
似乎我无法从包含的布局文件中访问 $view 对象,这可能是一个愚蠢的问题,但现在我真的无法意识到为什么不应该工作......
有人可以解释一下 php 在这种情况下是如何工作的吗?我做错了什么?