Welcome Guest, Not a member yet? Register   Sign In
Question about CI_Base class
#1

[eluser]Michael Ekoka[/eluser]
I noticed that the CI_Base class forks for the 2 versions:

This is the v5 CI_Base content:

Code:
class CI_Base {

    private static $instance;

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

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

function &get_instance()
{
    return CI_Base::get_instance();
}
now this is v4:
Code:
class CI_Base extends CI_Loader {

    function CI_Base()
    {
        // This allows syntax like $this->load->foo() to work
        parent::CI_Loader();
        $this->load =& $this;
        
        // This allows resources used within controller constructors to work
        global $OBJ;
        $OBJ = $this->load; // Do NOT use a reference.
    }
}

function &get_instance()
{
    global $CI, $OBJ;
    
    if (is_object($CI))
    {
        return $CI;
    }
    
    return $OBJ->load;
}

My question is the following, is there a particular reason why we couldn't adapt v4 CI_Base to emulate v5 like this. This is strictly out of technical curiosity. I'm not a php4 expert, I use php5 mostly, so I'm a bit shady with the pass by reference syntax in php4, feel free to correct.
Code:
class CI_Base {

    function CI_Base()
    {
        self::instance(& $this);
    }
    
    function instance($i=null){
        static $instance;
        if($i){
            $instance = $i;
        }
        return $instance;        
    }
    
    function &get_instance()
    {
        return self::instance();
    }
}

function &get_instance(){
    return CI_Base::get_instance();
}
#2

[eluser]stijn1989[/eluser]
easy, in PHP4 their is no static and so the scope resolution operator (:Smile won't work either.
#3

[eluser]Michael Ekoka[/eluser]
Hi stijn1989

Quote:easy, in PHP4 their is no static and so the scope resolution operator (:Smile won’t work either.

I'm not so sure about that. I do remember from my past readings on the singleton pattern in php4, that a variable can remain static inside a method or a function. Also, revisiting an old database class that I had created for a past application, I see that I did use the pattern and was able to call my connections throughout my models with DB::query($query).

In the present case, it is not exactly a singleton as most of us know it, but rather some variant of it and I feel that I overdid it a little bit. I'll simplify:
Code:
class CI_Base {

    function CI_Base()
    {
        self::instance(& $this);
    }
    
    function & instance($i=null){
        static $instance;
        if($i){
            $instance = & $i;
        }
        return $instance;        
    }
}

function &get_instance(){
    return & CI_Base::instance();
}

I guess I'm just wondering as to why the pattern was not applied here. Like I said I'm just an occasional php4 coder, but I've been going through the code and some things are just like puzzles, you need answers or you'll dream of them at night.




Theme © iAndrew 2016 - Forum software by © MyBB