Get_Instance() in Codeigniter: Why Assign It to a Variable

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

As far as I know, it's a matter of convenience more than anything. Chances are that you will be using the CI super object a lot in your libraries so why not assign it to a variable to make it a little easier to work with?

There are a few other things to consider...

  1. If you put this method in a helper, that method becomes a dependency for any class you are using it in. This might not be a big deal for you, but if you want to share libraries with anyone else they may not be happy about the dependency, especially since there is already a standard way of handling this in the CI community.
  2. There is a slight impact on performance because you are calling get_instance() every time you use the helper rather than storing its result in a variable.
  3. Since this is a helper method that is supposed to save you time, for anyone who is working mostly in the core MVC files of CI, setting up a helper like this would take longer than just setting it to a variable in the few places you need it.

explain $CI =& get_instance();

It's basically a Singleton Design Pattern that uses a function instead of a static method.

To look deeper, check out the source code

So basically, it doesn't enforce the singleton, but it's a shortcut to a public function...

Edit: Actually, now I understand. For PHP4 compatibility they had to do a double-global-variable-hack to get it to return the references properly. Otherwise the references would get all screwed up. And since PHP4 didn't have support for static methods (well, properly anyway), using the function was the better way. So it still exists for legacy reasons...

So if your app is PHP5 only, there should be nothing wrong with doing CI_Base::get_instance(); instead, it's identical...

how to use get_instance() in custom library file in codeigniter

Try the below code. there are some changes suggested

class Commonlib {
private $ci;
public function __construct()
{
$this -> ci=& get_instance();
$this -> ci->load->database();
}
function getcountries(){

return $this -> ci->db->get("countries")->result();
}
function cities(){
return $this -> ci->db->get("cities")->result();
}
}

Note: In your old code $db in __construct() method will have scope with in that method only. For get the ci object globally with in that entire class I used $this.

Codeigniter get_instance function from external script

the only thing you've to do is to try to create an instance...
The best way to do this is to actually include the index.php - but to suppress the output

the following should work

ob_start();
require_once('./index.php');
ob_end_clean();

var_dump($CI->load->helper('url'));

How to get get_instance in abstract class in Codeigniter?

In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance).
First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

Once you've assigned the object to a variable, you'll use that variable instead of $this:

$CI =& get_instance(); 
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

etc.

Note: You'll notice that the above get_instance() function is being passed by reference:

$CI =& get_instance();

What does this function do in codeigniter?

This function checks wheather the $var data is from post or get method using codeigniter's instance.

get_instance is not working in codeigniter helper function

Functions have their own scope in PHP, you have to pass CI into your function or declare it within your function

e.g.

if ( !function_exists('refresh_token'))
{
function refresh_token()
{
$CI = get_instance();
return $CI->security->get_csrf_hash() ;
}
}

Take a look at https://www.php.net/manual/en/language.variables.scope.php for more informations

Autocomplete Codeigniter when using get_instance()

To re-bind get_instance() to your IDE’s autocomplete, you'll need inform it that’s an instance of the native controller for CodeIgniter. Which isCI_Controller:

/**
* Example MyClass Library within
* /application/libraries/
**/
class MyClass {

/**
* @var CI_Controller
**/
private $ci;

/**
* Init MyClass
**/
public function __construct()
{
$this->ci =& get_instance();
}
}

Codeigniter: Get Instance

Ok, so everything in CodeIgniter runs through the super-magic $this variable. This only works for classes, as $this basically defines the current class.

Your controller is a class, so $this is there, allowing you to do $this->load->model('whatever');

In models, you are also using a class. It is slightly different here, as $this only contains useful stuff as you are extending from Model. Still, $this is still valid.

When you are using a helper or a library, you need to find that "instance" or $this equivalent.

$ci =& get_instance();

…makes $ci contain the exact same stuff/code/usefulness as $this, even though you are not in a class, or not in a class that inherits it.

That's an explanation for total beginners after 2 pints, so it's either wrong or about right. ;-)



Related Topics



Leave a reply



Submit