Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter - best practice library with many classes

I'm building a library for our CodeIgniter app, but it requires many classes (currently I'm at 12).

Is there a best practice for packaging these many clients into one library. So I can just make one call to load it. i.e:

$this->load->library('soaplibrary');

Thanks!

like image 294
markymark Avatar asked Feb 11 '11 16:02

markymark


3 Answers

As Summer points out, they have handled this situation somewhat elegantly in CI 2.0 with the concept of Drivers.

With a Driver, you actually create a subdirectory within your 'libraries' directory that contains your 'super' class, and another directory for 'child' classes. Better visual representation of the structure...

enter image description here

This was taken from Here.

and once you have constructed your library, here is the documentation on how to use it.

like image 146
jondavidjohn Avatar answered Nov 08 '22 14:11

jondavidjohn


In CI 2.0, there are drivers to handle this situation. Good luck!

like image 29
Summer Avatar answered Nov 08 '22 13:11

Summer


In CodeIgniter 3.1.9 when you load a library file, all classes in this file are included into code.

Let's say in soaplibrary.php you have

class SoapLibrary {
   public function someMethod(...

class Test {
   public function anotherMethod(...

In your controller you can do:

$this->load->library('soaplibrary');

//now on you can do
$this->soaplibrary->someMethod();

//but also
$test = new Test();
$test->anotherMethod();

CodeIgniter attempts to call the constructor of class SoapLibrary, hence a class with that name must be in there.

like image 1
Marco Demaio Avatar answered Nov 08 '22 14:11

Marco Demaio