access controller variables in a model |
[eluser]Unknown[/eluser]
i dont know, i have been trying to find a logic in it for hours ... but i cant find out from a model i can access a variable declare in the calling controller i can use it, echo it, and display his content but i can not do an isset on this variable it will return false to be able to test if a variable exists in my controller from my model, i have to use & get_instance(), and then i ll be able to test if the var exists or not even if i display the defined variables in my model, it wont display my controller's variable, but still i ll be able to use those variables ??? can anyone explain me please cheers
[eluser]Unknown[/eluser]
thanks but i am not sure that answer my question here is some code to visualize what i am saying Code: class test extends CI_Controller { if you meant that my ctrl_var inside the model is only a reference to the controller's variable , so i believe that modifying it in the model would also modify it in the controller. and also i believe that isset an existing reference return true ???
[eluser]Unknown[/eluser]
Stumbled on this while we were investigating a similar situation. Took us a while to figure it out: the base CI_Model class contains this magic method: Code: function __get($key) This means that accessing any Model property that does not exist will fall back to accessing the same named property from the controller. So the code: Code: class Model { Here's more on the PHP magic methods: http://etutorials.org/Server+Administrat...l+Methods/ It probably is buried somewhere in the CI documentation or their changelog that they are doing this but couldn't find the place. Hope this helps! Vlad
[eluser]Aken[/eluser]
It's still not a great idea to have models require properties from the controller, since models are supposed to be "universal" of sorts, and not dependent on other things. What if you create a new controller and forget to create the properties? Best to either pass those variables as parameters, or set up setter/getter methods for storing properties in your model. |
Welcome Guest, Not a member yet? Register Sign In |