title: PHP/CodeIgniter - Setting variables in __construct(), but they're not accessible from other functions
body: <p>I'm happy a bit of a variable scoping problem. Maybe I just need more coffee...</p>
  
  <p>Here's my (simplified) code - this is in CodeIgniter 2:</p>
  
  <pre><code>class Agent extends CI_Controller {     
  
  public function __construct()
  {
      parent::__construct();
  
      $this-&gt;load-&gt;model('agent_model');
  
      // Get preliminary data that will be often-used in Agent functions
      $user   = $this-&gt;my_auth_library-&gt;get_user();
      $agent  = $this-&gt;agent_model-&gt;get_agent($user-&gt;id);
  }
  
  public function index()
  {       
      $this-&gt;template-&gt;set('info', $this-&gt;agent_model-&gt;get_info($agent-&gt;id));
  
      $this-&gt;template-&gt;build('agent/welcome');
  }
  </code></pre>
  
  <p>Unfortunately, when I run the index function, I'm told:</p>
  
  <pre><code>A PHP Error was encountered
  
  Severity: Notice
  Message: Undefined variable: agent
  Filename: controllers/agent.php
  Line Number: 51
  </code></pre>
  
  <p>Line 51 is the first line of the index function. What's going wrong? Is this a scope issue or something else?</p>
  
  <p>Thanks!</p>