• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use my own library multiple times

#1
Hi,

for an price calculator i want to create my own library. The calculator gets 2 parameters, an product-object and an rate-object. For multiple products (e.g. in a list) i have to call this lib multiple times. what is the right way in order to use the library or is there a better way for doing that?

Normally i would use
$a = new PriceCalculator(Product1,Rate1);
$b = new PriceCalculator(Product2,Rate1);
Reply

#2
Suppose in your library you had a file called Library_test that looked like this:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class 
Library_test {
 
 public function test_function($a$b)
 
 {
 
      // just add them up
 
      $c $a $b;
 
      return $c;
 
 }


Then in your controller you would load the library and use this:

PHP Code:
 $this->load->library('library_test');
 
 
 $result $this->library_test->test_function(24); 

I hope that helps,

Best wishes,

Paul.
Reply

#3
Hi Paul,

thank you. I know this, but in the CI-doku i read, that i can pass parameters:

PHP Code:
$this->load->library('library_test','param'); 

But if i need this library multiple times, my first thougt was to call this line in a loop. Is this a good way or should i create a function like you?

PHP Code:
$this->load->library('library_test');
$this->library_test->set('param1','param2'// like a constructor 
Reply

#4
Hi,

I don't know then in that case. Sorry if I misunderstood. I think you should only load the library once. So for me, I would use a function in the library, but I might be wrong here.

Hope you get it sorted,

Best wishes,

Paul.
Reply

#5
Hi,

i have to use this lib for every combination of a product and a rate and on the page i show many combinations of this.
Reply

#6
When a library is loaded, it is a singleton. Loading it multiple times returns the first (one & only).

Approach that will work for you, with your source in application/libraries/Pricecalculator.php :

$this->load->library('pricecalculator');
$a = new Pricecalculator(Product1,Rate1);
$b = new Pricecalculator(Product2,Rate1);
Reply

#7
Hi,

(11-02-2015, 01:49 PM)ciadmin Wrote: Approach that will work for you, with your source in application/libraries/Pricecalculator.php :

$this->load->library('pricecalculator');
$a = new Pricecalculator(Product1,Rate1);
$b = new Pricecalculator(Product2,Rate1);

is this the right way or is there a better solution how to do that? Is this a case for a library?
Reply

#8
You can automatically load the library for your entire project in application/autoload.php
you can also load it in the constructor of your controller, and it will be available to all the methods in that controller.

also if these are relatively simple methods you could create a Helper, autoload it, and then you can call the methods directly from anywhere.
Reply

#9
ok, thank you. :-)
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.