Hello,
I am trying to create a basic MVC framwork in order to learn OOP and inheritance better. I’m running into a bit of a problem though.
When I am in IndexController and try to use access $this-> I get the following error. Fatal error: Using $this when not in object context.
I am not sure what the deal is I want to be able to access the registy object through it’s magic methods and the magic __get() method declared in the abstract controller object but it’s not working. Am i missing something fundamental about inheritance and how it works?
Any help would be appreciated.
I have a base abstract controller:
<?php
abstract class Stx_Controller_Controller {
protected $_model;
protected $_registry;
public function __construct()
{
$this->_registry = Stx_Registry::getInstance();
}
abstract public function index();
protected function loadModel($model)
{
$model = $model . 'Model';
$_model = new $model;
}
protected function loadView($view)
{
}
public function __get($key)
{
if (isset( $this->_registry->$key ) )
{
return $this->_registry->$key;
}
return false;
}
}
The base controller in the system folders
<?php
class Stx_Controller extends Stx_Controller_Controller {
public function __construct() {
parent::__construct();
}
public function index(){}
}
And the controller that is in the webroot.
<?php
class IndexController extends Stx_Controller{
public function __construct() {
parent::__construct();
}
public function index()
{
echo 'hello';
$this->_registry->$db = new Stx_Database();
}
public function hello()
{
echo 'hello';
$this->_registry->$db = new Stx_Database();
$this->db->execute();
}
}
And this is my registry
<?php
class Stx_Registry extends Stx_Registry_RegistryAbstract {
protected function __construct() {
parent::__construct();
}
public static function getInstance()
{
if (self::$_instance === null)
{
self::$_instance = new Stx_Registry;
}
return self::$_instance;
}
}
<?php
abstract class Stx_Registry_RegistryAbstract {
protected static $_instance;
private $_storage;
protected function __construct() {}
abstract public static function getInstance();
public function __set($key, $value)
{
$this->_storage[$key] = $value;
}
public function __get($key)
{
if ( isset( $this->_storage[$key] ) )
{
return $this->_storage[$key];
}
return false;
}
}