Welcome Guest, Not a member yet? Register   Sign In
CI_Base inquiry
#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.


Messages In This Thread
CI_Base inquiry - by El Forum - 12-03-2007, 10:40 AM
CI_Base inquiry - by El Forum - 12-03-2007, 11:29 AM
CI_Base inquiry - by El Forum - 12-03-2007, 11:33 AM
CI_Base inquiry - by El Forum - 12-03-2007, 11:42 AM
CI_Base inquiry - by El Forum - 12-03-2007, 03:58 PM
CI_Base inquiry - by El Forum - 12-05-2007, 04:18 AM
CI_Base inquiry - by El Forum - 12-06-2007, 03:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB