Inheritance not recognizing parent class's variables

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;
    }
}

@Fiddlestx

I ran your code on my server and am not triggering the error. Can you show how you are wiring this when you get the error?

I attached a copy of my webroot.

This error occurs when you visit /index/hello. However, it doesn’t always occur on first run which leads me to believe that there are other issues as well.

@Fiddlestx

The attachments are still pending approval. I will try to take a look over lunch (around 12;00 eastern time)

Attachments still not available

The attachment’s now available…

Thank you :slight_smile:

@Fiddlestx

[COLOR=#464646]I tried for about 40 minutes this morning to get you application registered on my server; however i ran into a lot of path dependencies and was unable to get this running.

The magic method __get() you are using is trying to assign a string you pass it as a [/COLOR]property[COLOR=#464646] that it cannot assign because the property does not exist. As I planned to run and debug where this happens and I was unable to, then you will have to output what values are being assigned to __get() and then review each property that __get() handles to see which one is the culprit.

Steve[/COLOR]

Thank you for trying, I will look into that. This is my first foray into objects with php so I was pretty sure it was going to go up in flames :slight_smile:

@Fiddlestx

The one comment I have about your architecture is that they have a lot of dependencies that will become headaches once your start to fill in some of your classes.

As the old PHP Application Design forum was merged with this one then one recommendation is to search for controllers. I seem to remember some good examples that were not too in-depth that I learned from - Some from Rhen, Dr. Livingston, LastCraft, and kyberfabrikken.

I will do that, thank you very much. :slight_smile: