title: Accessing variables in CodeIgniter classes
body: <p>The question I originally had has been asked (and answered) several times before<sup><a href="https://stackoverflow.com/questions/13470428/php-call-variable-from-another-function-in-class">1</a>,<a href="https://stackoverflow.com/questions/2483675/in-php-how-to-call-a-variable-inside-one-function-that-was-defined-previously">2</a>,<a href="https://stackoverflow.com/questions/14082589/php-accessing-variables-within-a-function-defined-in-another-function?rq=1">3</a>,<a href="https://stackoverflow.com/questions/17860930/php-how-to-get-variable-from-function-in-another-function">4</a></sup> : how do I call a variable defined in a different function in the same class - but couldn't get the following to work:</p>
  
  <pre><code>class my_model extends CI_Model {
  
    public $myvar;
  
    public function test1()
    {
      $this-&gt;myvar = "Hello world";
    }
  
    public function test2()
    {
      return strtoupper($this-&gt;myvar);
      //And have tried:
      // return my_model::test1()-&gt;$myvar
    }
  
  }
  </code></pre>
  
  <p>Changing <code>test1</code> to act as a <code>setter</code> function (and calling that function to access the variables) does work; changing my <code>test1</code> to:</p>
  
  <pre><code>public test1()
  {
    $this-&gt;myvar = "Hello world";
    return $this-&gt;myvar;
  }
  </code></pre>
  
  <p>and <code>test2</code> to:</p>
  
  <pre><code>public function test2()
  {
    $a = $this-&gt;test1();
    return strtoupper($a);
  } 
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/8275294/how-to-access-variables-from-other-methods-inside-the-same-class-in-php?rq=1">This question</a> has a comment that <code>controller methods in CodeIgniter work slightly differently from regular PHP class methods</code>.  Is this also the case for <code>model</code> classes: if so is this the reason I need to call <code>test1</code> first (and that variables in other methods aren't accessible), or is there something else I've failed to understand?!</p>
  
  <p><strong><em>Real life example</em></strong></p>
  
  <p>This is what I'm actually trying to do - please tell me if there's a better way to do it!</p>
  
  <p>I was going to split up the methods which run a <code>select *</code> query and a query which returns a <code>count</code>, and since I'm using CI's <code>QueryBuilder</code> class this seemed the obvious solution:</p>
  
  <pre><code>class my_model extends CI_Model {
  
    public function __construct()
    {
      $this-&gt;load-&gt;database();
    }
  
    public function test1()
    {
      return $this-&gt;db-&gt;get('Table1');
    }
  
    public function test2()
    {
      return $this-&gt;test1()-&gt;result_array();
    }
  
    public function test3()
    {
      return $this-&gt;test1()-&gt;num_rows();
    }
  }
  </code></pre>
  
  <p>However, <a href="https://stackoverflow.com/a/34557762/889604">cale b</a> made a comment that this might feel a bit <em>jenky</em> - how else would it be done? </p>