How to Create and Handle an Array of Models or Objects in Codeigniter


In most object oriented programming situations we want to dynamically create varied number of instances(objects) of a specific class. Therefore its impossible to rely on predefined variable. The best solution is to use an array to hold the objects dynamically created.

Those of who are not familiar with codeigniter (CI) framework might ask why is it so difficult to populate an array with objects. In php its as simple as assigning a value to an array. In codeigniter the object class are called models and they can only be loaded with the CI load function. Once loaded, the loaded model/class becomes available(global) through out the controller class. This limitation makes it difficult to create an object as an array element.

Conventional PHP way of creating array of objects

    USER is class (a model in codeigniter). Not shown here.

foreach($_REQUEST['users'] as $userid){
	$usersArray[] = new USER($userid);
//example of using it
foreach($usersArray as $theUser){

Codeigniter work-around for creating variable number of objects dynamically

The code below creates a list of objects from a form post. You might be creating it differently.

    USER is a model
foreach($_REQUEST['users'] as $userid){
        $objectName = 'u'.$userid; // each object is given a distinct name
	$this->load->model('USER', $objectName); // load the model with a distinct name
	$usersArray[] = $objectName; //store the object/model names in an array.

‘u’ is included in case the userid does not start with an alphabet or an underscore. The custom model name must be in line with PHP variable naming conventions and rules.

This is an example of how you would be using the objects. The most important line of code is the 2nd line. In normal case the property within an object is called without the ‘$’ sign. Here what the compiler would do is to take the value withing the variable $this->theUser and the value is considered as a variable.

foreach($usersArrayas $theUser){
	$this->$theUser->show(); // the value of the variable is considered as a variable. So its like a variable inside a variable

for example:
if ‘$this->theUser’ has the value ‘u1234’ then ‘$this->$theUser’ will be equal to ‘$this->u1234’.

If you have any questions to doubts feel free to ask me in the comment section. If You have a better solution or idea please be kind enough to share it.

No related content found.

2 Responses to How to Create and Handle an Array of Models or Objects in Codeigniter

  1. A very useful piece of information at the right moment. Thanks Alfie for taking the effort in sharing this.

  2. I’m facing exactly the same problem and also considering this method. Another thing i came up with is use a php object, that is later used in a CI_Model to store.

    $person = new Person(‘jan’, 55);


    But this will need some more coding. Because Key Value Programming is not really a PHP thingy.

    Another way is instead of using a Person class, just use an array (where we can use the key as the value)

    array(‘Person’ => array(‘age’ => 44, ‘name’ =>’jan’));

    Lets see if the community answers our question. I asked it on Stackoverflow. See:

Leave a Reply

Your email address will not be published. Required fields are marked *