Welcome Guest, Not a member yet? Register   Sign In
Error on $ci =& get_instance();
#1

[eluser]PermanaJ[/eluser]
I got error on line '$ci =& get_instance();'

the error message is
Quote:Fatal error: Call to undefined function get_instance()

Anybody know how to solve the error? I want to extends Language class so it load language base on cookie

Code:
class MY_Language extends CI_Language{
    
    function MY_Language() {
        parent::CI_Language();
        
        $ci =& get_instance();
        $language = get_cookie('language');
        if( $language ){
            $ci->config->set_item('language',$language);
        }else{
            set_cookie('language', $ci->config->item('language'), 1447);
        }
        $ci->lang->load('myword', $ci->config->item('language'));

    }
}
#2

[eluser]xwero[/eluser]
The language class is loaded before the CI instance exists, that is why you get the error.

I would do what you want with a post_controller_constructor hook function
Code:
function cookie_language()
{
   $CI =& get_instance();

   if(isset($_COOKIE['language']))
   {
      // check here if the cookie has a valid value
      $CI->config->set_item('language',$_COOKIE['language']);
   }
   else
   {
      setcookie('language',$CI->config->item('language'),1447);
   }
}

I have a few questions :
Where do the functions get_cookie and set_cookie come from?
Why do you need to set the $language variable after you set a cookie?
And why do you load a language file in the constructor?
#3

[eluser]PermanaJ[/eluser]
- it is from cookie helper ...

- do you mean the line .. '$language = $ci->config->item('language');' ? sorry it was just for test .. i'll edit the code ...

- I want the language file loaded after system check whether there are cookie named 'language'

btw, thanks for the solution .. I have not discover the hooks feature ...
#4

[eluser]xwero[/eluser]
You better keep the code you write as close to php as possible. As you see in the code i wrote you don't need to do difficult things to get and set a cookie in native php code.

Loading language files should be done just in time otherwise you end up with files you load for no reason because their values get overwritten by other files.
#5

[eluser]PermanaJ[/eluser]
Thanks for the advice .. I just want to try all code igniter's feature ...

btw, I have try the hooks ...

Code:
function do_language() {
    $CI =& get_instance();

    $language_cookie = config_item('cookie_prefix').config_item('language_cookie');
    $language = config_item('language');

    if(isset($_COOKIE[$language_cookie])){
        $CI->config->set_item('language',$_COOKIE[$language_cookie]);
        $language = $_COOKIE[$language_cookie];
    }
    $path = '/';
    $expire = time() + 604800;

    setcookie($language_cookie, $language, $expire, $path);

    $CI->lang->load(config_item('language_file'),config_item('language'));
}

It works when the hook point is 'post_controller_constructor', but when I try to try at hook point 'pre_controller' it result an error message :
Quote:Fatal error: Call to a member function set_item() on a non-object in D:\Project\PHP\myapp\application\hooks\myhooks.php on line 9

I need to set the language before the controller initialized because I load language file at controller ...
#6

[eluser]xwero[/eluser]
the ci instance doesn't exist pre controller, this is because the controller is the CI instance. so your helper isn't loaded either.

load your files as late as possible. autloading language files is the option you should consider if there is no other possibility because then the files are loaded in each controller of your application.
If you load language files in the constructor this means all methods use them. You can extend the language class to fetch the files that already are loaded, clean the internal storage and load them again with the language you just set in the hook function. So you call the method in the extended language class in your hook function.
If you load the language files in your controller methods everything should work.
#7

[eluser]PermanaJ[/eluser]
[quote author="xwero" date="1237070508"]... You can extend the language class to fetch the files that already are loaded, clean the internal storage and load them again with the language you just set in the hook function. So you call the method in the extended language class in your hook function ... [/quote]

I guess i'll try that .. thanks a lot ...




Theme © iAndrew 2016 - Forum software by © MyBB