Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call a codeigniter library inside another library file?

Tags:

codeigniter

I want to call a function in a library inside another library which is written by me. Is it possible to do this in codeigniter? If so, can anyone explain how to do that?

like image 673
Stranger Avatar asked Mar 02 '12 06:03

Stranger


Video Answer


2 Answers

You could do;


$CI =& get_instance();

$CI->load->library('your_library');
$CI->your_library->do_something();  

like image 86
Sudhir Bastakoti Avatar answered Sep 28 '22 12:09

Sudhir Bastakoti


Typically, you reference the Codeigniter object (the current controller, technically) by using get_instance(). Often you'll want to assign it to a property of your library, like this:

class My_Library {

    private $CI;

    function __construct()
    {
        // Assign by reference with "&" so we don't create a copy
        $this->CI = &get_instance();
    }

    function do()
    {
        $var = $this->CI->my_other_library->get();
        // etc. 
    }
}

Just make sure the other library is loaded or in your config/autoload.php.

like image 38
Wesley Murch Avatar answered Sep 28 '22 12:09

Wesley Murch