Multiple classes in Codeigniter Multiple classes in Codeigniter codeigniter codeigniter

Multiple classes in Codeigniter


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.