CodeIgniter Forums

Full Version: Loading db from an extended library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hello,
I'm using CI 1.7.2 with PHP 5.
I'm trying to load data from a db to populate an array inside a library (language) I extended (ie: MY_language).
I tried doing the
Code:
$CI =& get_instance()
to be able to load db class but get an fatal error stating that:<br>
Code:
Call to undefined function get_instance() in /path/application/libraries/MY_Language.php on line [29]
The line #29 it's when it's trying reference the CI object.

Why do I want to do this? I need to be able to load the supported languages (supported by my app) to 3 different files, and want it to be automatic instead of hand-edited like now.

Any ideas?

Thanks,

Fefo

El Forum

[eluser]placid2000[/eluser]
Hello, I m having a similar issue. have you already found a solution for this?

I'm trying to load a database function in my extended MY_Form_Validation class but it says:
Fatal error: Call to a member function get_where() on a non-object in..

the constructor looks like this:


function MY_Form_Validation($config)
{
parent::CI_Form_validation($config);
$CI =& get_instance();
$CI->load->database();
}

Greetz
Cyril

El Forum

[eluser]danmontgomery[/eluser]
[quote author="placid2000" date="1268684384"]Hello, I m having a similar issue. have you already found a solution for this?

I'm trying to load a database function in my extended MY_Form_Validation class but it says:
Fatal error: Call to a member function get_where() on a non-object in..

the constructor looks like this:


function MY_Form_Validation($config)
{
parent::CI_Form_validation($config);
$CI =& get_instance();
$CI->load->database();
}

Greetz
Cyril[/quote]

You're not declaring it as a class member... You need to use the $this keyword if you want to access it in other class methods.

http://php.net/manual/en/language.variables.scope.php
http://www.php.net/manual/en/language.oop5.basic.php

El Forum

[eluser]Phil Sturgeon[/eluser]
Code:
$IN        =& load_class('Input');
$LANG    =& load_class('Language');

/*
* ------------------------------------------------------
*  Load the app controller and local controller
* ------------------------------------------------------
*
*  Note: Due to the poor object handling in PHP 4 we'll
*  conditionally load different versions of the base
*  class.  Retaining PHP 4 compatibility requires a bit of a hack.
*
*  Note: The Loader class needs to be included first
*
*/
if ( ! is_php('5.0.0'))
{
    load_class('Loader', FALSE);
    require(BASEPATH.'codeigniter/Base4'.EXT);
}
else
{
    require(BASEPATH.'codeigniter/Base5'.EXT);
}

Spot the issue there.

get_instance() is set in Base4/Base5.php so this is not an error, just part of the architecture.

You'd be better off making a MY_Controller which calls the database, then make a MY_Language that has a set_line() method to set your new lang items.