Saturday

Adding Your own Code to the CI

Adding Your own Code to the CI
'Super-Object'
You contribute to the process of building the 'super-object' as you write your own
code. Suppose you have written a model called 'status', which contains two class
variables of its own, $one and $two, and a constructor that assigns them values of 1
and 2 respectively. Let's examine what happens when you load this model.

The 'instance' class includes a variable 'load', which is a copy (by reference) of the
object CI_Loader. So the code you write in your controller is:

$this->load->model($status)

In other words, take the class variable 'load' of the current CI super-class ('this') and
use its method 'model'. This actually references the 'model' function in the 'loader'
class (/system/libraries/loader.php) and that says:

function model($model, $name = '')
{
if ($model == '')
return;

$obj =& get_instance();
$obj->_ci_load_model($model, $name);$obj->_ci_load_model($model, $name);
}}

(The $name variable in this code is there in case you want to load your model under
an alias. I don't know why you should want to do this; perhaps it's wanted by the
police in several other namespaces.)

As you can see, the model is loaded by reference into the Instance class. Because
get_instance() is a singleton method, you're always referencing the same instance
of the Instance class.