views:

259

answers:

2

Hello, people!

I have this piece of code:

class MY_Language extends CI_Language {

   function MY_Language()
   {
       parent::CI_Language();        

       $CI =& get_instance();

       $CI->load->model('language_model');

       $languages = $this->language_model->get_languages();

       print_r($languages);
   }

}

But I keep getting "Fatal error: Call to undefined function get_instance() in C:\xampp\htdocs\application\libraries\MY_Language.php on line 44". Any ideas? Thanks in advance!

+2  A: 

The framework creates the Language object prior to the CodeIgniter base object. So at that point in the code you won't yet be able to use the base object. In fact, the codeigniter/Base5.php file has yet to be included. This file defines the get_instance function. Thats why you are getting this particular error.

If you look in the codeigniter/CodeIgniter.php file you'll see that

$LANG =& load_class('Language');

comes prior to

require(BASEPATH.'codeigniter/Base5'.EXT);

Edit

Based on your comment below, I think you should be able to get an instance of the DB as follows. This was dug from the database function of the Loader class.

require_once BASEPATH.'database/DB'.EXT;
$db = DB('', false);
Stephen Curran
Thanks, Stephen!Do you know any other way to access the database within a library using codeigniter functionalities?
ci
I dug into the framework to see how the DB is loaded. I edited my answer with some code to fetch an instance of the DB class. I think it should work, but I didn't get a chance to test it, so no guarantees! :-S
Stephen Curran
It worked!Thanks again, Stephen!
ci
Great. Glad to help!
Stephen Curran
A: 

Thanks, Stephen! I'll test it as soon as I get home. :)

ci