1

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 :)

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

It looked fine, but it doesn't work :( It gives 500 error, which means the library itself isn't loaded.
Did you change COUNT to your number of iterations or a variable containing them? I've tried this code (with a different library) and it's running fine.
Yes I did, didn't work. I found the way to do it. Let me post it.
0

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);
}

1 Comment

Thank you. Your answer made me think in a different way, found the solution. (I didn't try your answer)
0

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!!

3 Comments

I'm glad to hear it helped you :-). But if you would think further you would see to use only one instance is more effective than always create and free multiple instances. (maybe not in this case with small objects, but in a high loaded system ;-) )
True I totally agree :) But I'm anyways storing the values need into an array, so I think this would be fine even when the system is big ;)
Unfortunately that doesn't work because Yes it unsets the object but doesn't tell CI to reload a new one for it, it keeps the old one cached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.