Reputation: 1313
Before I start just letting you know that I'm new to OOP programming so take it easy on me.
I'm currently working on a PHP framework and I've been able to route my traffic through one entry point. This is the structure below..
index ----> bootstrap ----> router ----> controller
Here's the main controller...
class controller {
protected $_model;
protected $_controller;
protected $_id;
protected $_view;
function __construct($controller,$model,$view,$id=NULL) {
$this->_controller = $controller;
$this->_id = $id;
$this->_model = $model;
$this->_view = $view;
$this->$model = new $model;
$this->_view = new view;
}
function filter($data) {
$data = trim(htmlentities(strip_tags($data)));
if (get_magic_quotes_gpc())
$data = stripslashes($data);
$data = mysql_real_escape_string($data);
return $data;
}
function set($name,$value) {
$this->_view->set($name,$value);
}
function __destruct(){
$this->_view->render();
}
}
In my construct function it's suppose to set the...
$this->$model = new $model;
and $this->_view = new view;
But when I run either $this->_view->render();
or $gamerequests_array = $this->main_model->tab_query($tab);
It says the object doesn't exist. Here's the exact error message I get.
Notice: Undefined property: main_controller::$main_model
and
Fatal error: Call to a member function tab_query() on a non-object
So at this point I know that the problem is the framework setting up the new objects. I've been trying to figure this out and at this point I need some help.
If anyone needs extra code feel free to request it, I'll post it.
Upvotes: 1
Views: 237
Reputation: 48897
From this line:
$this->$model = new $model;
It appears your passing in a string and using it both as the property name and the class name. Perhaps you're passing in a name that is not exactly main_model
. Though variable/property names are case-sensitive, class names are not. So, they're may be a case mismatch. Pass $this
through get_object_vars
to see what properties are available. Keep in mind that dynamically adding properties in this way will make them public.
These lines should be examined as well:
$this->_view = $view;
$this->_view = new view;
You're overwriting the property. I'm sure this is unintentional and may be part of the problem you're experiencing. Perhaps you're trying to use the same approach as you did with the controller (store name, use for instantiation and access).
Upvotes: 2
Reputation: 971
$this->$model = new $model;
This could only possibly work if the value for $model also happens to be a property that has already been set. Which means it can only contain the values '_controller', '_id', '_model' and '_view' or it will kick out an error (Notice: Undefined property: main_controller::$main_model and Fatal error: Call to a member function tab_query() on a non-object
)- even if it has one of those values, it will still overwrite whatever you set the property as.
If you are trying to dynamically set the view as something, you may wish to pass $view
into the View constructor as an argument:
$this->_view = new view($view);
You can further extend that by setting different behaviours for different values of $view
through inheritance via either a global function with a case switch returning the appropriate object or a method of the View class.
Upvotes: 0