Welcome Guest, Not a member yet? Register   Sign In
access controller variables in a model

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


Passing by Reference

thanks but i am not sure that answer my question
here is some code to visualize what i am saying

class test extends CI_Controller {
var $ctrl_var=10;

public function index(){
  echo  $this->ctrl_var; //here display 10
  echo  $this->ctrl_var; //here display 10

class test_model extends CI_Model{

var $my_model_var="Halo";

function __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

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:
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:
class Model {
public function test() {

class Controller {
$this->foo = 'Hello World';
.. 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+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!


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.

Theme © iAndrew 2016 - Forum software by © MyBB