CodeIgniter Forums
Problem loading custom library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Problem loading custom library (/showthread.php?tid=29568)

Pages: 1 2


Problem loading custom library - El Forum - 04-13-2010

[eluser]frost9928[/eluser]
Hi guys,

I'm having a problem getting a library to load. I have a total of three libraries that are add-ons to the base CI system. The first one is an access control library. It loads and works just fine. The second is a library to calculate sales tax. It won't load no matter what I do. It's structure is as follows.

taxcalc.php
Code:
<?php
class Taxcalc extends Controller {

   function Taxcalc() {
       parent::Controller();
   }

   function get_state_tax(zip) {

   }
}
?>

The above structure is the same for the access control library which does work and is also the same for the UPS shipping library which doesn't work either. Each file has been placed in the /system/application/libraries folder with lowercase file names and upper cased class names as is CI standard.

I'm loading them with:

Code:
$this->load->library('taxcalc');

And trying to access them with:

Code:
$this->taxcalc->some_function();

Can anyone tell me if I'm doing something wrong or if I may have a problem with my CI install.

Thanks


Problem loading custom library - El Forum - 04-14-2010

[eluser]Mareshal[/eluser]
try this code. paste it in application/libraries/first.php

Code:
class First{
    function test(){
        echo "first->test";
    }
}

then in your welcome controller: $this->load->library('First');

$this->first->test();

and get back with an answer


Problem loading custom library - El Forum - 04-14-2010

[eluser]adamp1[/eluser]
Well since its a library first of all it won't extend a controller. A library should be as follows
Code:
class Taxcalc
{
    function Taxcalc()
    {
    }

    ....
}

So change that in both your library's. For more details please read this page in the user guide http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html


Problem loading custom library - El Forum - 04-14-2010

[eluser]ranjeet_sangle[/eluser]
I had similar problem and got it solved by auto loading the library in config/autoload.php. You can try this out and check that you are not loading you library more that once in same controller..


Problem loading custom library - El Forum - 04-14-2010

[eluser]frost9928[/eluser]
[quote author="adamp1" date="1271247242"]Well since its a library first of all it won't extend a controller. A library should be as follows
Code:
class Taxcalc
{
    function Taxcalc()
    {
    }

    ....
}

So change that in both your library's. For more details please read this page in the user guide http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html[/quote]


That's what I tried originally and I swapped back to that style and unfortunately it didn't work either. Auto loading the library worked fine but I hate to think that I'd have to do that for every library I write because it just makes the script so much longer than it really needs to be even though it just stays dormant.


Problem loading custom library - El Forum - 04-14-2010

[eluser]ranjeet_sangle[/eluser]
if you dont want to autoload then just check out your code for multiple calls to $this->load->library.. I think it will work fine..


Problem loading custom library - El Forum - 04-15-2010

[eluser]frost9928[/eluser]
Oh so the problem is declaring them line by line...

Code:
$this->load->library('access');
$this->load->library('ups');

When I should be 'daisy chaining' for lack of a better phrase.

Code:
$this->load->library('access','ups');

Am I understanding you right?

That works quite well. I thank you much.


Problem loading custom library - El Forum - 04-15-2010

[eluser]adamp1[/eluser]
I didn't think the second option would work. Never seen that functionality in CI before.


Problem loading custom library - El Forum - 04-15-2010

[eluser]danmontgomery[/eluser]
Like adamp1 said, the 2nd parameter in library() is to specify a config file to load with the library, not another library to load.


Problem loading custom library - El Forum - 04-15-2010

[eluser]ranjeet_sangle[/eluser]
bro you are getting me all wrong ...


I was just saying that you must have loaded the same library more than once...

like
Code:
class Some_controller extends Controller
{function index()
{
$this->load->library('ups');
//some code
$this->load->library('ups');
}//index()
}//class

and there's no problem in calling like $this->load->library('ups','some_other_lib');

Sorry if my suggestion are not worth for you..!!