Codeigniter – How Do You Create Multiple Instances of a Library Class in CodeIgniter

classcodeigniterinstance

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.

Best Solution

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.