Welcome Guest, Not a member yet? Register   Sign In
Loading a library in a loop
#1

[eluser]asyafrudin[/eluser]
Hi.

I realized that if I wanted to use a 3rd party class in CodeIgniter, I have to use it as part of CodeIgniter's library. It happens that the class I'm using will be used inside a loop. Here's the snippet:

Code:
for ($i = 0; $i < some_number; $i++)
{
    $obj = new Myclass();
    //Codes using $obj ...
}

Knowing that loading a class in CodeIgniter means loading a library, the above code should look like this:

Code:
for ($i = 0; $i < some_number; $i++)
{
    $this->load->library('Myclass');
    //Codes using $this->myclass ...
}

But then I realize both code does not do the exact same thing. The first code will create a new instance of Myclass for each iteration, but it's not like that with the second code. It seems that in the second, the library will actually be loaded only in the first iteration.

The above results in unexpected behavior because I really need to load a new object for each iteration. Is there any way to make sure that the second code actually creates a new $this->myclass object?

Thanks for the help.


Messages In This Thread
Loading a library in a loop - by El Forum - 05-03-2011, 06:49 PM
Loading a library in a loop - by El Forum - 05-04-2011, 01:06 AM
Loading a library in a loop - by El Forum - 05-04-2011, 02:36 AM
Loading a library in a loop - by El Forum - 05-04-2011, 02:38 AM
Loading a library in a loop - by El Forum - 05-04-2011, 03:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB