Welcome Guest, Not a member yet? Register   Sign In
loading a library twice
#1

[eluser]AtlantixMedia[/eluser]
ok, I think I found what's wrong with my code. I have a Validate library which I load somewhere at the very top of my script. This library can accept optional parameters. the FIRST time I load this library I do not need to pass any arguments. Then, I need to use this library again further down my script and need to pass arguments this time. However, the arguments are not being passed the second time as CI prevents a library from being loaded twice? how can I get around this?

thanks
#2

[eluser]AtlantixMedia[/eluser]
of course, when the master controller loads the library the first time, it does now know whether the library will be needed again further down. so passing the parameters the very first time is not an option here.
#3

[eluser]gtech[/eluser]
I assume you are passing variables to the libraries constructor on the load.

could you not create a function in your library that will set the class variables, so the first time you load the library you pass in the params which then calls an initialize function which set the class variables.

The second time you do not need to load the library, instead you just call the initialize function directly.. check out the calender library for an example.

Code:
class Someclass {

    var $testvar;

    function Someclass($config = array())
    {
        if (count($config) > 0)
        {
            $this->initialize($config);
        }
    }

    function initialize($config = array())
    {
        foreach ($config as $key => $val)
        {
            if (isset($this->$key))
            {
                $this->$key = $val;
            }
        }
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB