title: Re load a library in CodeIgniter that requires custom config
body: <p>I have a library in CI that requires a dynamic config object to set it up.</p>
  
  <p>This same library is required further down the line from another library that does <em>not</em> have access to the config object.</p>
  
  <p>At the moment, this works fine, as loading the library for a second time returns the singleton that was initially created.  However, this isn't very nice, as it only works because the library was previously called with the config object.   It's not portable, as if the library is NOT called initially then it will error.</p>
  
  <p>This is the set up....</p>
  
  <p>In <code>controller_a</code></p>
  
  <pre><code>$this-&gt;load-&gt;library('lib_a');
  $this-&gt;lib_a-&gt;do_something();
  </code></pre>
  
  <p>Then in <code>lib_a</code> we dynamically make a config object..</p>
  
  <pre><code>// loads lib_b
  $this-&gt;load-&gt;library('lib_b');
  
  // and loads `shared_lib`
  $my_config = $this-&gt;build_config();
  $this-&gt;load-&gt;library('shared_lib', $my_config);
  
  
  // call something in shared_lib
  $this-&gt;shared_lib-&gt;do_something();
  
  
  // call something in lib_b
  $this-&gt;lib_b-&gt;do_something();
  </code></pre>
  
  <p>Then in <code>lib_b</code> we also call <code>shared_lib</code></p>
  
  <pre><code>// Load the shared lib, but we don't have access to the config.
  // But this is fine as it returns the existing singleton instance
  $this-&gt;load-&gt;library('shared_lib');
  $this-&gt;shared_lib-&gt;do_something_else();
  </code></pre>
  
  <p>What are the alternatives / better ways to do this?</p>
  
  <ul>
  <li><p>Somehow add some validation / error handling so I can throw a <code>Exception</code> if the library is loaded and it HASNT been configured yet?</p></li>
  <li><p>Pass the config around all the function calls?</p></li>
  <li><p>Create a static class to create and retrieve the singleton?<br>
  Create at the start with <code>Shared_lib::create( $config );</code><br>
  Then retrieve with <code>Shared_lib::get_instance();</code></p>
  
  <p>But this pretty much has the same issues...</p></li>
  <li><p>Create the lib and pass that around instead?   </p></li>
  </ul>
  
  <p>There must be a cleaner way to solve this...</p>