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?
I want to create an array of objects, so what I did was to
Share
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:
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.