PHP Overloading, singleton instance
Posted
by
jamalali81
on Stack Overflow
See other posts from Stack Overflow
or by jamalali81
Published on 2012-11-09T02:04:30Z
Indexed on
2012/11/09
11:02 UTC
Read the original article
Hit count: 261
I've sort of created my own MVC framework and am curious as to how other frameworks can send properties from the "controller" to the "view". Zend does something along the lines of $this->view->name = 'value';
My code is:
file: 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';
}
}
file: 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');
}
}
file: 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]);
}
}
In my services_hosting.phtml "view" file I have:
<img src="<?php echo $this->p->banner ?>" />
This just does not work. Am I doing something fundamentally wrong or is my logic incorrect? I seem to be going round in circles at the moment. Any help would be very much appreciated.
© Stack Overflow or respective owner