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

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


Messages In This Thread
access controller variables in a model - by El Forum - 02-07-2012, 08:33 AM
access controller variables in a model - by El Forum - 02-07-2012, 01:38 PM
access controller variables in a model - by El Forum - 02-08-2012, 08:42 AM
access controller variables in a model - by El Forum - 08-21-2012, 12:57 PM
access controller variables in a model - by El Forum - 08-21-2012, 02:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB