I want to create an array of objects, so what I did was to create a library but I can't figure out how to actually dynamically create instances of it in a loop and store each instance in an array. Can anyone tell me please?
To create 100 objects, you just need to loop from 0 to 99, creating an object every time and storing it in the array.
class Foo { ... }
$fooArray = array();
for ($i = 0; $i < 100; $i++) {
$fooArray[] = new Foo();
}
I'm not sure what this question has to do with CodeIgniter. Is there more you're not mentioning?
By design, loading a CodeIgniter library can only be done once. Subsequent attempts to load the same library are ignored. You can (in a way) get around this by telling CI to instantiate the class with a different name every time you load another copy of the library (see the answer to this question)
A better solution is probably to create your class yourself, instead of using CI's library loading mechanism. That way you can create and store as many copies as you need.
EDIT: I'd suggest leaving the class in the libraries directory, and just using PHP's include() to make it available to your models/controllers where needed.
As for accessing CodeIgniter from within your class, you can do it using the following code:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
The get_instance() function returns the CodeIgniter super object, and once it's assigned to the $CI variable, you can access any of CI's methods just like you would from within a model or controller, except using $CI instead of $this. See this link for more information.
Please, check this link: I think is the best way to do it:
http://stackoverflow.com/questions/1513685/creating-an-object-from-a-class-in-codeigniter
It uses Code Igniter code, but it let you use the "new" word, like any other OOP application.
I hope this help you.