1

Possible Duplicate:
get_instance() in Codeigniter: Why assign it to a variable?

I have a controller, and it have lots of functions, I would like to separate them into different cmd object. But in controller, I can do the $this magic, for example, I can call something like this:

$this->form_validation->run('myAction');

But when I move this code to an object, which is not a controller, so, I called the function like this:

    $CI =& get_instance();
    $CI->form_validation->run('myAction');

I realize that they can run, and the result is exactly what I want. But, my question is, is these two operation or codes have any different? What is the get_instance() black magic inside? Did the controller's $this refer to the same get_instance()? Thanks.

Community
  • 1
  • 1
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • based on the answer given here, i daresay they are the same [1]: http://stackoverflow.com/questions/2819435/codeigniter-get-instance – He Hui Jan 25 '13 at 08:37
  • from the name itself get_instance() an instance of the class that has been initialized. $this refers to the methods loaded when extending the core controller CI_Controller – tomexsans Jan 25 '13 at 08:41

1 Answers1

1

using the $ci = &get_instance(), you're directly using the codeigniter native libraries, you're not making a copy of it. The "$this" command can be used only in inside the controllers, so assign the CI object to a variable is the only way you've got to go inside the Ci core libraries outside the controller. Note that the "$this" command inside a model refer to the model object itself.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Andrea Golin
  • 128
  • 1
  • 7