Welcome Guest, Not a member yet? Register   Sign In
Static loader for CI libraries PHP5 only
#1

[eluser]wiredesignz[/eluser]
For a bit of difference you can use this method to load CI libraries from any context.
Code:
spl_autoload_register('MY_Loader::autoload');

class MY_Loader extends CI_Loader {    

    /** CI Library base class autoload **/
    public static function autoload($class) {
        
        /* don't autoload CI_ or MY_ prefixed classes */
        if (strstr($class, 'CI_') OR strstr($class, 'MY_')) return;

        /* load the library base class */    
        if ( ! class_exists('CI_'.$class, FALSE))
            load_class($class, FALSE);
            
        $class_var = strtolower($class);
        $core = 'static function instance() {
                    $ci = get_instance();
                    if ( ! isset($ci->'.$class_var.')) $ci->load->library(\''.$class_var.'\');
                    return $ci->'.$class_var.';
                }';
        
        /* extend the MY_ class and return an instance */                
        if (class_exists('MY_'.$class, FALSE)) {
            return eval('class '.$class.' extends MY_'.$class.'{'.$core.'}');
        }

        /* extend the CI_ class and return an instance */                
        if (class_exists('CI_'.$class, FALSE)) {
            eval('class '.$class.' extends CI_'.$class.'{'.$core.'}');
        }
    }
}
Libraries can be accessed like so.
Code:
Session::instance()->set_userdata();
Form_validation::instance()->set_rules();

//or

$this->session = Session::instance();
$this->cache_path = Config::instance()->item('cache_path');

Play with this as you wish, it most likely needs some improvement yet.




Theme © iAndrew 2016 - Forum software by © MyBB