I'm doing a learning exercise where I'm building a pretty basic MVC framework. I'm really only doing this to learn more about OOP and it's pros, cons and common pitfalls.
I'm trying to replicate a behaviour, or syntax might be more correct, similar to the very popular framework codeigniter.
That is, I want to manually be able to load a Model from inside my Controller.
Here's how I want to perform it, and subsequently use it.
$this->load->model("mymodel");
$this->mymodel->some_function();
I have my loader working, it tries to run the class and this is how the load->model code looks like
public function model($model)
{
if(file_exists(APPLICATION.'models/'.$model.'.php'))
{
include(APPLICATION.'models/'.$model.'.php');
$this->{$model} = new $model;
}
}
The problem I am having is I get a error running this code saying that the class $model (this should be transformed into mymodel in this case) doesn't exist.
How do I make it so that $model translates into mymodel so the code would perform a action as such: new mymodel;
Thanks for any help, I'm quite the novice in OOP so I might have gotten confused here but really cannot seem to figure this out :/
$this->{$model}
does, however, translate into $this->mymodel
.
Instead of creating a new thread, I'll add to this.
My next issue has already arisen, since it's basically a followup problem I'll add it here too.
$this->mymodel->some_function() returns the following error;
Notice: Undefined property: Home::$mymodel in C:\xampp\htdocs\application\controllers\home.php on line 16
This error shows when running $this->mymodel->some_function();
Home is the loaded controller.
Hultin