I am trying to create a simple MVC my personal use and I could really use an answer to this simple question
class theParent extends grandParent{
protected $hello = "Hello World";
public function __construct() {
parent::__construct();
}
public function route_to($where) {
call_user_func(array("Child", $where), $this);
}
}
class Child extends theParent {
public function __construct() {
parent::__construct();
}
public function index($var) {
echo $this->hello;
}
}
$x = new theParent();
$x->route_to('index');
Now Child::index() this throws a fatal error: Using $this when not in object context but if I were to use echo $var->hello, it works just fine.
I know I can use $var to access all properties in the parent, but I would rather use $this.
You don’t have an instance of
Childto call a non-static method upon when you’re doing$x->route_to('index');The way you’re calling the method, without having made an instance first, is implied static.There are two ways to correct it. Either make the
Childclass’s methods static:…or make an instance of the child class for the parent to use:
Of course, both of these samples are rather generic and useless, but you see the concept at hand.