0

I want to extend default CodeIgniter's class CI_Lang by creating a file /core/MY_Lang.php, and inside that file I need to load a custom configuration file:

class MY_Lang extends CI_Lang {

    var $CI;

    // Default langs
    var $languages = array(
        'en' => 'english',
        'fr' => 'french'
    );

    public function __construct(){
        parent::__construct();

        global $CFG;

        $this->CI  =& get_instance(); // NOT WORKING

        // Load custom config file from /config/languages.php
        $this->languages = $this->CI->config->item('languages');

    }

}

and /config/languages.php custom config file:

$config['languages'] = array(
    'en' => 'english',
    'fr' => 'french',
    'ru' => 'russian',
    'sk' => 'slovak'
);

Whenever I call for get_instance - I get an error telling me Fatal error: Class 'CI_Controller' not found in D:\wamp\www\ci2exp6\system\core\CodeIgniter.php on line 233

Please, could someone clarify what's wrong in my code ?

1
  • 1
    get_instance() was provided through CI_Controller. It's not a general method for all CI classes. Commented Sep 6, 2013 at 6:42

1 Answer 1

4

Inside of a core library you need to call it as follows:

$CFG =& load_class('Config', 'core');
$CFG->load('languages', true);

$languages = $CFG->config['languages'];
$english   = $languages['en'];
//etc...

That should do the trick :)

Updated config file from comment:

$config['en'] = 'english';
$config['fr'] = 'french';
$config['ru'] = 'russian';
$config['sk'] = 'slovak';
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for answer, but it would be a core-hack? If so, I would like to not touch the core files
It's the standard way to access core classes in codeigniter core libs.
Thanks for your idea, I did it this way: $CFG->load('languages');. Could you modify your answer so I will accept it.
You also need to fix your config file: should be as follows... $config['en'] = 'english'; $config['fr'] = 'french';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.