1

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.

3
  • You are overwriting $this->_view and $this->$model can't work too, because it's an object. Commented Apr 25, 2012 at 20:17
  • you could use an existing framework rather than rolling your own. Commented Apr 25, 2012 at 20:18
  • I'm teaching myself to build frameworks, It's not like i'm trying to finish something in time. Commented Apr 25, 2012 at 20:20

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

You're right, me overwriting the property is a result of me trying to fix things.
array(5) { ["_model"]=> string(10) "main_model" ["_controller"]=> string(15) "main_controller" ["_id"]=> NULL ["_view"]=> object(view)#3 (1) { ["variables:protected"]=> array(0) { } } ["main_model"]=> object(main_model)#2 (3) { ["_model:protected"]=> string(10) "main_model" ["_dbHandle:protected"]=> bool(false) ["_result:protected"]=> NULL } } How am I doing ?
I see the main_model object. Where and how are you trying to access it?
Here's how I'm calling the object ---> $gamerequests_array = $this->_model->tab_query($tab); and this is the error Call to a member function tab_query() on a non-object
Should do: $this->main_model->tab_query($tab) instead of $this->_model. _model is just a string. If you wish to use the string to call it you can do: $this->{$this->_model}->tab_query($tab);
0
$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.

2 Comments

Alright so at this point I changed $this->$model = new $model; into $this->_model = new $model; and it appears to be working, the only problem is that I'm having troubles calling that object. Here's how I'm calling it ---> $gamerequests_array = $this->_model->tab_query($tab); and here's the error ---> Call to a member function tab_query() on a non-object.
Try ` $gamerequests_array = new controller ($controller ,$model ,$view ,$id=NULL);` -followed by ` $gamerequests_array->_model->tab_query($tab);`. I'm assuming that $gamerequests_array is part of the running script and not a local variable in the object itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.