fedejp
fedejp

Reputation: 970

How to load library within another library?

there is a similar question to this from a couple years ago, but I didn't quite understand the answer (link to the question)

My problem is the following: I've created a library that I'm calling from a controller, and within that library I'm using the image manipulation functions that CodeIgniter offers. But that's obviously a library. From a controller there's no problem: $this->load->library('image_lib', $config); the $config variable has of course the parameters to create the image. The question is: How can I call that library (and pass the $config array) from within another library?

Thanks in advance.

Upvotes: 1

Views: 4148

Answers (2)

Owen
Owen

Reputation: 790

Your $this var will have lost it's CodeIgniter resources, to use the CodeIgniter Resources you'll have to create another instance of it get_instance();

$CI =& get_instance();

Now you can use $CI as you would $this

You can read more about it here under "Utilizing CodeIgniter Resources within Your Library" http://codeigniter.com/user_guide/general/creating_libraries.html

Upvotes: 1

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

Here how you do it in helpers and other libraries:

$ci =& get_instance();

$config = array(
    //...
);
$ci->load->library('image_lib', $config);

$ci->image_lib->whatever();

What you do here is get the instance of the CodeIgniter singleton object and work with it, as libraries can't use $this because $this used in libraries points at themselves. Simply do all your usual things, but using $ci-> instead of $this->

Upvotes: 4

Related Questions