Explain $Ci =& Get_Instance();

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

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.

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

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. ;-)

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

CodeIgniter &get_instance() not working in php5.6 version?

You can try with this, change on CodeIgniter.php line 75

set_error_handler('_exception_handler');

to

set_exception_handler('_exception_handler');

Update:

If this is not working, maybe you can check this others things:

  1. Check if is a DB error (it's weird, but sometimes the DB error
    is shown as 'CI_Controller not found', for instance, with a bad db
    name).
  2. Check if changing: $config['log_threshold'] = 0; works
    for you
  3. Reinstall the whole CI framework system folder.

Here and here have the same problem, you can check all the answers.

If none of these works, try to post some code and context to your controller.

Hope it helps!

Codeigniter not loading CI super object

You cannot access the $CI instance in a pre_controller hook - as per the docs:

pre_controller hook Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done..

It is the CI Controllerwhich allows access to get_instance(). Until a controller is instantiated, there is nothing to get.

Try post_controller_constructor instead and see if that gets you the desired results.

In system/Core/Controller.php:

class CI_Controller {

// <snip>

public static function &get_instance()
{
return self::$instance;
}

}
// END Controller class

CodeIgniter: load model in each library or kind of 'pass' the model from within the controller to a library

get_instance is a native way suggested by official docs:

for easier access to model object I would define a property in your library:

public $_model = FALSE;

somewhere in your library consructor:

$CI =& get_instance();
$CI->load->model('model_name');
$this->_model = $CI->model_name;

then you can access the model in any library method by calling $this->_model->method();



Related Topics



Leave a reply



Submit