CodeIgniter Forums

Full Version: convert string to object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]zeratool[/eluser]
I have posted some codes(model, library, controller) here :

http://pastebin.com/f71555678

The error i got is
" faultCode1faultStringFatal error:Call to a member function add() on a non-object in /var/html/application/controllers/testmodel.php on line 17 "

in pastebin, it's line 62.


The idea is have one abstract Model object where i can just extend it so every model i make will just call add() from line 13(pastebin) to save data.

Error occured cuz this->$model is a string,so the question is how to make it an "object" model so it can call the inherited method add().

THank you.

El Forum

[eluser]xwero[/eluser]
Code:
$this->$this->model->add()
That looks like a train wreck if i ever seen one Smile

I think this will do the trick
Code:
$this->{$this->model}->add()

El Forum

[eluser]zeratool[/eluser]
Update pastebin for syntax highlighting:

http://pastebin.com/f4fb888f2



Thanks, but the error changed to
"faultCode1faultStringFatal error:Cannot access empty property in /var/html/application/libraries/ModelManager.php on line 18"

it's line 18 too on pastebin.


Additional error message on the original snippet

"Severity: 4096

Message: Object of class testmodel could not be converted to string

Filename: controllers/testmodel.php

Line Number: 18"

El Forum

[eluser]Randy Casburn[/eluser]
Shouldn't your $model be declared private rather than public in your controller? Otherwise there is likely to be a scoping issue. That is another problem that rear up eventually.

El Forum

[eluser]zeratool[/eluser]
that is another thread, lets stick first on the topic, thanks

El Forum

[eluser]Randy Casburn[/eluser]
Alright brain quiz - when xwero already showed you that you were pointing at the wrong object once. It is likely your object reference is still inaccurate. That may or may not be due to a scoping issue. But hey I'll go away "cuz" you want that to be in another thread. cu.

El Forum

[eluser]zeratool[/eluser]
sorry if i offended you, it doenst meant that way. peace! i just need this thing to work. thanks!

El Forum

[eluser]Scriptor[/eluser]
In pastebin line 19:
Code:
$ok = $this->CI->$model->save();
Your $model variable in the ModelManager class is empty, you never set it to the actual model name.

El Forum

[eluser]Randy Casburn[/eluser]
zeratool wasn't calling $model from ModelManager Scriptor, zeratool was calling it from the scope of CI ($this->CI->$model->save. The comment in the code even leads you to believe there was trouble retrieving the contents of the variable by that name. This is what I was trying to point out earlier. The scoping is confused and makes it difficult to figure out what he's trying to accomplish.

El Forum

[eluser]Scriptor[/eluser]
You don't get what I meant, $model is referenced to inside the add method of ModelManager. PHP is trying to find that variable inside the scope of the add method, but it's undefined, so it comes out as an 'empty property'. It doesn't matter if he's doing CI->$model, or does Foo->$model, or AnyClass->$model, the scope is still in ModelManager, so that's where PHP is going to try to find the $model variable.
Pages: 1 2