5

I'd like to create several instances of a class in CodeIgniter. I have created my class as a library, but cannot figure out the syntax to use to create more than one instance.

imlouisrussell
  • 936
  • 11
  • 30
Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116

3 Answers3

15

From the CodeIgniter users guide:

CI Users Guide: Loader Class

Assigning a Library to a different object name

If the third (optional) parameter is blank, the library will usually be assigned to an object with the same name as the library. For example, if the library is named Session, it will be assigned to a variable named $this->session.

If you prefer to set your own class names you can pass its value to the third parameter:

$this->load->library('session', '',
 'my_session');

Session class is now accessed. using:

$this->my_session

I think that's what you're looking for.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
GloryFish
  • 13,078
  • 16
  • 53
  • 43
8

I know this thread is long passed, but it was one of the questions I came across while looking for my answer. So here's my solution...

It's PHP. Create your class as a library, load it using the standard CI Loader Class, but use it like you would in a regular PHP script.

Build your class:

class My_class {

    var $number;

    public function __construct($given_number){
        $number = $given_number;
    }

    public function set_new_num($given_number){
        $number = $given_number;
    }
}

Load it:

// This will load the code so PHP can create an instance of the class
$this->load->library('My_class');

Then instantiate and use the object where needed:

$num = new My_class(24);

echo $num->number;
// OUTPUT: 24

$num->set_new_num(12);

echo $num->number;
// OUTPUT: 12

The only time I use $this->my_class is to make calls to static functions that I code.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Birdman
  • 724
  • 5
  • 9
  • 1
    doesn't work. You cannot define a paramater in the construct and then load the library like that and initiate an instance in CI. it will throw an error since it expects you to load it like this: $this->load->library('My_class', $given_number) – mixix Dec 15 '13 at 16:07
  • May I ask how we can get the instance back in order to store it to an array for example? [inside the controller] $this->someclass; gives the current instance every time? cause I did the following with diffrent $params for each on but what I got as a result was the first object not two diffrent: $this->load->library('SomeClass', $params); print_r($this->someclass); $this->load->library('SomeClass', $params); print_r($this->someclass); – limitcracker Jun 23 '14 at 13:07
6

Sorry for reviving this topic but I think I might have something reasonable to add.

You can do this to add multiple instances of a class. I don't know if it violates Codeigniter standard usage anyhow but seems more Codeigniterish than loading a library (which creates $this->library_name which isn't used) and then making 2 MORE instances with the "new" keyword.

$this->load->library( 'my_library', '', 'instance1' );
$this->load->library( 'my_library', '', 'instance2' );

$this->instance1->my_class_variable = 1; 
$this->instance2->my_class_variable = 2; 

echo $this->instance1->my_class_variable; // outputs 1
echo $this->instance2->my_class_variable; // outputs 2

I use this in my code to generate different menus. I have a "menu" class and different instances for each menu, with different menu items in each.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Liviu Gelea
  • 361
  • 2
  • 9