PHP重载
我已经创建了自己的框架来开发我的个人投资组合网站。我(非常)松散地基于Zend框架,我对Zend框架相当熟悉。除了在控制器内设置变量外,我还设法使其他工作正常进行,然后可以在.phtml文件中使用这些变量。这就是我目前拥有的(例如,这里的url是www.mydomain.com/services/hosting): front.phpPHP重载,php,overloading,Php,Overloading,我已经创建了自己的框架来开发我的个人投资组合网站。我(非常)松散地基于Zend框架,我对Zend框架相当熟悉。除了在控制器内设置变量外,我还设法使其他工作正常进行,然后可以在.phtml文件中使用这些变量。这就是我目前拥有的(例如,这里的url是www.mydomain.com/services/hosting): front.php class front { private $sControllerName = 'home'; private $sActionName = '
class front {
private $sControllerName = 'home';
private $sActionName = 'index';
function __construct() {
$this->view = new viewProperties();
$aUriParts = $this->getUriParts();
if (!empty($aUriParts[0])) {
$this->sControllerName = $aUriParts[0];
}
if (!empty($aUriParts[1])) {
$this->sActionName = $aUriParts[1];
}
$this->constructController();
include('path/to/views/view.phtml');
}
private function constructController() {
$sContFile = 'controllers/'.$this->sControllerName.'.php';
if (@include($sContFile)) {
$c = new $this->sControllerName();
// Action method, if exists
$this->doAction($c);
}
}
public function getElement($name) {
include('public_html/elements/'.$name);
}
public function renderContent() {
$sViewFile = 'path/to/views/'.$this->sControllerName;
if ($this->sActionName) {
$sViewFile .= '/'.$this->sActionName.'.phtml';
} else {
$sViewFile .= '.php';
}
if (!@include($sViewFile)) {
$this->render404();
}
}
private function doAction($c) {
$sActionFunc = str_replace('-', '_', $this->sActionName.'Action');
if (method_exists($c,$sActionFunc)) {
$c->$sActionFunc();
}
}
private function render404() {
include('path/to/views/404.phtml');
}
private function getUriParts() {
$sUri = $_SERVER['REQUEST_URI'];
$sUri = trim($sUri, "/");
$sUri = str_replace("-", "_", $sUri);
$aUriParts = explode("/", $sUri);
return $aUriParts;
}
}
viewProperties.php
class viewProperties {
static $_instance = null;
private $data = array();
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __set($name, $value) {
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name) {
echo "Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}
public function __isset($name) {
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
public function __unset($name) {
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
}
services.php
class services extends controller {
public function indexAction() {
$this->view->banner->src = '/path/to/images/banners/home_02.jpg';
$this->view->banner->alt = 'banner title';
}
public function hostingAction() {
$this->view->banner->src = '/path/to/images/banners/home_02.jpg';
$this->view->banner->alt = 'banner title';
}
}
在hosting.phtml中,我有:
<img src="<?php echo $this->view->banner->src ?>" alt="<?php echo $this->view->banner->title ?>" />
view->banner->src?>“alt=”“/>
我如何在控制器中设置属性(不仅仅是横幅),然后在视图中检索它们?非常感谢您的帮助/指导。由于您没有确切说明您遇到的问题,我只能从代码中猜测 我猜您当前无法设置/检索横幅属性 如果是,请尝试以下方法:
class services extends controller {
public function indexAction() {
$this->view->banner = array
(
'src' => '/path/to/images/banners/home_02.jpg',
'alt' => 'banner title'
);
}
public function hostingAction() {
$this->view->banner = array
(
'src' => '/path/to/images/banners/home_02.jpg',
'alt' => 'banner title'
);
}
}
<img src="<?php echo $this->view->banner['src'] ?>" alt="<?php echo $this->view->banner['title'] ?>" />
[编辑]在完成前填写并提交
[编辑]用可能的解决方案展开答案。问题的确切含义是什么?很抱歉这么含糊。我如何设置属性(不仅仅是横幅)在控制器中,然后在视图中检索它们?非常感谢@David Farrel!我现在可以设置属性了。但是如何在我的视图中访问这些属性。phtml?view.phtl具有doctype和标记,我希望能够从控制器操作中自定义它们。我已经尝试放置
$view=viewProperties::getInstance();
在front.php中,但我什么也没有得到。我需要了解更多关于如何包含.phtml文件的信息,但我发现您在问题中提供的文件有$this->view->…
,除非您在类文件中执行此.phtml,$this
没有任何意义。如果它只是正常执行,然后在文件顶部添加
,将$view引入作用域(如果$view是全局变量,则不加入),并将$this->view->…
替换为$view->…
可能就可以了。请参阅我最初发布的代码中的front.php。view.phtml包含在view.phtml中的u-construct函数front.php中。我将函数调用“renderContent”“其中包括页面的特定内容。我建议明确您的实例变量-您似乎希望在前端
和控制器
类上显示$view。我会将“private$view”添加到这两个类中。另外,我没有看到任何代码将$view传递给控制器实例?您可能需要setView($view)
方法或将$view作为参数的构造函数。此外,您没有使用提供的附加ViewElement
class I更新原始帖子(如果关闭了,您可以更新吗?),但除非您这样做,否则您无法拥有设置view->banner->src
的魔力,因为之前没有定义banner。
class viewProperties extends ViewElement {
static $_instance = null;
private $data = array ();
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self ();
}
return self::$_instance;
}
}
class ViewElement {
private $data = array ();
public function __set($name, $value) {
echo "Setting '$name' to '$value'\n";
$this->data [$name] = $value;
}
public function __get($name) {
echo "Getting '$name'\n";
if (! array_key_exists ( $name, $this->data )) {
$this->data [$name] = new ViewElement();
}
return $this->data [$name];
}
public function __isset($name) {
echo "Is '$name' set?\n";
return isset ( $this->data [$name] );
}
public function __unset($name) {
echo "Unsetting '$name'\n";
unset ( $this->data [$name] );
}
}
$view = viewProperties::getInstance();
$view->boo = "hoo";
$view->foo->bar = "baz";
print ("boo = '{$view->boo}', foo->bar='{$view->foo->bar}'");