Welcome Guest, Not a member yet? Register   Sign In
CI_Base inquiry
#1

[eluser]stevefink[/eluser]
Hi all,

I'm currently experimenting with Michael Wales's auth plugin. Was just reading the source and ran into some CI code that I'm having difficulty understanding. This is the class CI_Base... it's documented as managing the CI super object, but I'm not entirely sure what this super object is?

Code:
class CI_Base {

    private static $instance;

    public function CI_Base()
    {
        self::$instance =& $this;
    }

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

Just trying to get a better understanding of the internals, that's all.

- sf
#2

[eluser]Michael Ekoka[/eluser]
I think the super object is the Controller in the current request.
#3

[eluser]stevefink[/eluser]
Interesting.

It appeared to me as an object which references all of the other current libraries loaded. When I see things such as $CI->db-> etc...

On a side note, 100+ posts for me! hehe.
#4

[eluser]Michael Ekoka[/eluser]
The CI_Base class keeps a reference to any object instantiated from a class that extends it (in this case Controllers).
So you have controllers that extends the Base class:
Code:
class Controller extends CI_Base {
...
    function Controller()
    {    
        parent::CI_Base();
        $this->_ci_initialize();
        log_message('debug', "Controller Class Initialized");
    }
}

You have the Base class that keeps a static reference to any object that is instantiated from a controller:
Code:
class CI_Base {

    private static $instance;

    public function CI_Base()
    {
        self::$instance =& $this;
    }

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

Then you have the get_instance() function that returns that specific instance and makes the current controller available throughout your application (models, views, helpers, libraries, plug-ins, etc):
Code:
function &get;_instance()
{
    return CI_Base::get_instance();
}

Note that this is the php5 version.
#5

[eluser]stevefink[/eluser]
Very cool. Thanks for the explanation. Sounds awfully technical, even though the code itself looks clean and simple. I suppose it's there for easier reference to certain parts of the application using the global CI object...
#6

[eluser]Grahack[/eluser]
To me the super object is not the current controller, but controllers are extensions of this super object.

It's what we call a singleton (it's a pattern). This object cannot be instanciated twice.
This to be able to change its properties and get the updated properties when calling it back (always passed as a reference).
#7

[eluser]Michael Ekoka[/eluser]
The CI_Base class is not exactly a Singleton. It looks like one, but I think it's closer to a Registry (a Registry class is usually implemented as a Singleton, but not in this case). If you try to instantiate a few controllers manually you'll find that you can create as much instances as you want. Each time an instance of a controller is created the CI_Base class registers it as the new super object.

From the top of my head this is what a generic Singleton looks like:
Code:
class Singleton{

    static $instance;
  
    private function Singleton(){}

    public static function instance(){
        if(is_a(self::$instance,'Singleton')){
            return self::$instance;
        }
        self::$instance = new Singleton();
        return self::$instance;
    }
}

In MVC, there generally is no reason to have 2 controllers for a single request and the convention followed in CI assumes the same. This is why the framework automatically creates an instance of the controller based on your route specifications and stores a reference to it in CI_Base. It is expected that you will not need to manually instantiate another controller.

The reason why I say that the super object is the current controller is that when you call the get_instance() function, it returns a reference to the current controller's object. From that object's reference you can call any properties and methods it publicly makes available, whether they were declared in the base class (Controller, MY_Controller) or the current extending class.




Theme © iAndrew 2016 - Forum software by © MyBB