title: Destruct Codeigniter Library Before View Output
body: <p>I have a custom Library in Codeigniter that creates session cookies on destruct.</p>
  
  <pre><code>public function __destruct()
  {
      $this-&gt;CI-&gt;session-&gt;set_userdata('user', $this-&gt;user);
  }
  </code></pre>
  
  <p>This fails because the headers have already been sent by the view before the class is destructed.</p>
  
  <p>I know I can manually destruct the library before the view is outputed.  My question is, is it possible to have my libraries destruct automatically before any headers are sent?</p>
  
  <p>Is there a hook I can use or some functionality in codeigniter that destroys all libraries/models?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After some trial and error, I found I can't manually destruct the library at the point I need. I tried a few approaches but the library still remains within scope and can't be unset.</p>