EllisLab text mark
Advanced Search
     
Multiple Classes inside a library
Posted: 04 October 2012 10:56 AM   [ Ignore ]
Joined: 2012-10-04
4 posts

I’m quite new to codeigniter, and was wondering is it possible with Codeigniter to have a library which contain’s multiple classes in?

I basically need to build a library to communicate with my merchant company’s API. The provider has given me the code to incorporate into a hard coded site, there is basically 3 files containing multiple classes in each: PaymentSystem.php, SOAP.php and Common.php

Is it possible to create 3 libraries containing all of the classes, load them into the controller and inside my controller function and create new instance for example like this:

class Payment_controller extends CI_Controller 
{
 
public function index()
    
{
     $this
->load->library('PaymentSystem');

        
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList();
        
// Or maybe I would have to do it like this sorry I am abit of a n00b!
        
$rgeplRequestGatewayEntryPointList $this->PaymentSystem->new RequestGatewayEntryPointList();
    
}

Or do I need to separate each class into individual libraries and load each one?

Or would i be better off just using require_once within the controller and leave the files as they are?

Or am i approaching this completely the wrong way!

 
Posted: 04 October 2012 11:03 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-06-27
134 posts

if it was me i would separate each class into it’s own file. You could even drop these class files into a sub directory in the libraries folder. DOn’t forget though that in order to load the lib’s you would need to get the CI instance:

class someclass{
private $ci;

function 
__construct()
{
$this
->ci =& get_instance();

then to load your libraries you would use:

$this->ci->load->library('subfolder/classname'); 
 Signature 

“I Reject Your Reality and Substitute My Own” - Adam Savage, M5 Inc

 
Posted: 04 October 2012 11:16 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-10-04
4 posts

Thanks so does that construct bit go in each of the library file?

or could i create a library which load’s all of the other libraries and just put that in that file?

 
Posted: 04 October 2012 11:23 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-06-27
134 posts

which ever lib needs to load first, put your load->library call in the __construct() function. And if the libraries need access to CI classes then you put the private $ci; in it. as well i find it good practice to ensure each class has a __construct() function.

 Signature 

“I Reject Your Reality and Substitute My Own” - Adam Savage, M5 Inc

 
Posted: 04 October 2012 11:54 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-10-04
4 posts

Brilliant thank you for your help

 
Posted: 04 October 2012 06:59 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-06-27
134 posts

no problem, glad to help.

 Signature 

“I Reject Your Reality and Substitute My Own” - Adam Savage, M5 Inc