我已经创建了自己的 MVC 框架,并且很好奇其他框架如何将属性从“控制器”发送到“视图”。Zend 做了一些事情,$this->view->name = 'value';
我的代码是:
文件:services_hosting.php
class services_hosting extends controller {
function __construct($sMvcName) {
parent::__construct($sMvcName);
$this->setViewSettings();
}
public function setViewSettings() {
$p = new property;
$p->banner = '/path/to/banners/home.jpg';
}
}
文件:controller.php
class controller {
public $sMvcName = "home";
function __construct($sMvcName) {
if ($sMvcName) {
$this->sMvcName = $sMvcName;
}
include('path/to/views/view.phtml');
}
public function renderContent() {
include('path/to/views/'.$this->sMvcName.'.phtml');
}
}
文件:property.php
class property {
private $data = array();
protected static $_instance = null;
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}
public function __isset($name) {
return isset($this->data[$name]);
}
public function __unset($name) {
unset($this->data[$name]);
}
}
在我的 services_hosting.phtml “视图”文件中,我有:
<img src="<?php echo $this->p->banner ?>" />
这只是行不通。我是在做一些根本错误的事情还是我的逻辑不正确?我现在似乎在兜圈子。任何帮助将不胜感激。