Arun Kumar
Arun Kumar

Reputation: 83

Codeigniter dynamic multiple instances for loading custom library

I wanted to create instances in a loop, which means the number of instances totally depends on the loop. I'm not able to achieve it. I've come across many posts regarding the same and was successful for,

$this->load->library('stlstats', $param, 'instance1');
$volume1 = $this->instance1->getVolume($unit);

$this->load->library('stlstats', $param, 'instance2');
$volume2 = $this->instance2->getVolume($unit);

//Don't bother about $param and $unit, those are pre-defined.

So, in the above code, I'm able to achieve getting different volumes. But I want it to be created each iteration. If I place the code inside the loop say,

$this->load->library('stlstats', $param, 'instance1');
$volume1 = $this->instance1->getVolume($unit);

And print $volume1, then the output is the same for all the iteration. Since I have no idea about the number of iterations of the loop, how can I achieve this?

Thank you all :)

Upvotes: 1

Views: 446

Answers (3)

Arun Kumar
Arun Kumar

Reputation: 83

The answer is very simple. I'm sure many might have come across this situation and I hope this answers.

$this->load->library('stlstats', $param, 'instance1');
$volume1 = $this->instance1->getVolume($unit);

Place the above code into the loop and at the end of the loop include this,

unset($this->instance1);

As simple as that :)

Thank you @Zaragoli your answer made me think in a right way :) Cheers!!

Upvotes: 0

Ron Dadon
Ron Dadon

Reputation: 2706

You placed this code in a loop:

$this->load->library('stlstats', $param, 'instance1');
$volume1 = $this->instance1->getVolume($unit);

But where is the loop variable?

You are always using the same instance alias 'instance1'.

A simple solution will be for example (COUNT is the number of iterations):

$volume = array(); // Store all volumes in array
for ($i = 1; $i < COUNT; $i++) {
    $instance = 'instance' . $i;
    $this->load->library('stlstats', $param, $instance);
    $volume[$i] = $this->$instance->getVolume($unit); // Add value to array
}

Upvotes: 1

Zaragoli
Zaragoli

Reputation: 686

CodeIgniter caches the already loaded libraries, so it will give back the same instance. By the way you shouldn't create multiple instances, instead you should re-organize your library code (instead of set the params in the constructor you should create a setter method) like this:

// library
class Stlstats {

    protected $params = array();
    public function __construct() {
    }
    public function setParam($params) {
        $this->params = $params;
    }
    public function getVolume($unit) {
        $this->params = $params;
        // example code:
        return ($unit * $params['distance']);
    }
}

// load once the library
$this->load->library('stlstats');
// test data
$unit = 22;
$all_params = array(
    array('distance'=>3),
    array('distance'=>5),
    array('distance'=>7),
);
// use it in loop
foreach($all_params as $params) {
    $this->stlstats->setParam($param);
    echo $this->stlstats->getVolume($unit);
}

Upvotes: 0

Related Questions