get_instance() is a function defined in the core files of CodeIgniter. You use it to get the singleton reference to the CodeIgniter super object when you are in a scope outside of the super object.
I'm pretty sure it's defined in base.php or something similar.
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...
this is a singleton structure to understand how the codeigniter loads the libraries and classes
<?php
/*
====================================
start of the loader class
====================================
*/
class Loader {
protected function _init_class($class){
$C = Controller::get_instance();
$name = strtolower($class);
$C->$name = new $class();
}
public function _class($library){
if(is_array($library)){
foreach($library as $class){
$this->library($class);
}
return;
}
if($library == ''){
return false;
}
$this->_init_class($library);
}
public function view ($param) {
echo $param;
}
}
/*
===============================
End of the loader class
==============================
*/
/*
===============================
start of core controller class
==============================
*/
class Controller {
private static $instance;
function __construct () {
self::$instance = $this;
$this->load = new Loader();
}
public static function get_instance(){
return self::$instance;
}
}
/*
===============================
end of the core controller class
===================================
*/
/*
====================================================
start of library sections (put all your library classes in this section)
=====================================================
*/
class MyLibrary {
private $c;
function __construct() {
$this->c = Controller::get_instance();
}
function say($sentence) {
$this->c->load->view($sentence);
}
}
/*
====================================================
End of the library sections
====================================================
*/
/*
============================================
start of controller section (put all your controller classes in this section)
===========================================
*/
class Foo extends Controller {
function __construct () {
parent::__construct();
$this->load->_class('MyLibrary');
}
function bar() {
$this->mylibrary->say('Hello World');
}
}
/*
==========================================
End of the controller sections
==========================================
*/
$foo = new Foo();
$foo->bar();
// this following code will not work
Class Car
{
$this->load->library('something');
$this->load->helper('something');
}
//this will work
Class Car
{
$CI=&get_instance();
$CI->load->library('something');
$CI->load->helper('something');
}
// Here $CI is a variable.