CodeIgniter Forums
access controller variables in a model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: access controller variables in a model (/thread-49055.html)



access controller variables in a model - El Forum - 02-07-2012

[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


access controller variables in a model - El Forum - 02-07-2012

[eluser]InsiteFX[/eluser]
Passing by Reference



access controller variables in a model - El Forum - 02-08-2012

[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 {
var $ctrl_var=10;

public function index(){
  echo  $this->ctrl_var; //here display 10
  $this->load->model('test_model');
  echo  $this->ctrl_var; //here display 10
}
}

class test_model extends CI_Model{

var $my_model_var="Halo";

function __construct(){
  parent::__construct();

  echo $this->ctrl_var; //here display 10
  var_dump(isset($this->ctrl_var)); //here display bool(false) !!????
  print_r(get_defined_vars()); //here display Array ( [this] => test_model Object ( [my_model_var] => Halo ) )
  var_dump(isset($GLOBALS['CI']->ctrl_var)); // here display bool(true)

  $this->ctrl_var = 20;

  echo $this->ctrl_var; //here display 20
  var_dump(isset($this->ctrl_var)); //here display bool(true)
  print_r(get_defined_vars()); //here display Array ( [this] => test_model Object ( [my_model_var] => Halo [ctrl_var] => 20 ) )
}
}

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
???


access controller variables in a model - El Forum - 08-21-2012

[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)
{
$CI =& get_instance();
return $CI->$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 {
public function test() {
  print($this->foo);
}
}

class Controller {
{
$this->foo = 'Hello World';
$this->load->model('Model');
$this->Model->test();
}
.. would print 'Hello World' because that fallback mechanism. It's not pass by reference so testing the variable returns false since it still technically doesn't exist in the model context. Once you declare that value in the model context, then that value will be returned instead of the fallback

Here's more on the PHP magic methods: http://etutorials.org/Server+Administration/upgrading+php+5/Chapter+2.+Object-Oriented+Programming/2.6+Magical+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


access controller variables in a model - El Forum - 08-21-2012

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