CodeIgniter Forums
codeigniter load library from within library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: codeigniter load library from within library (/showthread.php?tid=41933)



codeigniter load library from within library - El Forum - 05-21-2011

[eluser]haily_php[/eluser]
In codeigniter, what can I do to load library from within library?
Please help me.


codeigniter load library from within library - El Forum - 05-21-2011

[eluser]InsiteFX[/eluser]
Code:
private $CI;

public __construct()
{
    $this->CI = get_instance();
    $this->CI->load->library('library_name');
}

// then to access use
$this->CI->library_name->method();

InsiteFX


codeigniter load library from within library - El Forum - 07-12-2012

[eluser]bientek[/eluser]
get_instance seems to be undefined in the context of a library that replaces a CI native library. For example, in MY_Log...
Code:
function MY_Log()
{
    parent::CI_Log();
    $CI =& get_instance();
    $CI->load->library('test');
}

...produces: "Fatal error: Call to undefined function get_instance()..."


codeigniter load library from within library - El Forum - 07-12-2012

[eluser]CroNiX[/eluser]
your parent::CI_Log() should be parent::__construct(), and it should be in a __construct() itself, just like InsiteFX showed you. See the manual for Creating Libraries, especially the Replacing Native Libraries with Your Versions section

You aren't extending/replacing the native library correctly.


codeigniter load library from within library - El Forum - 07-12-2012

[eluser]bientek[/eluser]
Sorry, that's 1.7.2 code there. Regardless, one still cannot use get_instance() when replacing a native library, at least in this case.


codeigniter load library from within library - El Forum - 07-13-2012

[eluser]InsiteFX[/eluser]
It works here just fine.