title: Returning values from a Class Method
body: <p>I think this is a silly question - but I am not being able to fully grasp it :(</p>
  
  <p>Suppose I have a class called <code>Categories</code> which has a function called <code>getCategories()</code></p>
  
  <pre><code>class Categories
  {
  
    function __construct()
    {
  
    }
  
    function getCategories()
    {
      //sql to get all categories from db 
      return $categories;
    }
  }
  </code></pre>
  
  <p>And in other classes I will do something like:</p>
  
  <pre><code>$cats = new Categories();
  $cats-&gt;getCategories();
  </code></pre>
  
  <p>Now my question is, there are many places where I need to use the categories. So what if I created a variable called <code>$cats</code> as a member of <code>categories</code> class and loaded it in the constructor and just passed it back when anyone called <code>getCategories()</code> :</p>
  
  <pre><code>class Categories
  {
    var $cats; 
  
    function __construct()
    {
       $this-&gt;cats = $this-&gt;getCategories(); 
    }
  
    function getCategories()
    {
      //return loaded class variable instead of running db query again
      return $this-&gt;cats;
    }
  }
  </code></pre>
  
  <p>This way I do not need to hit database every time anyone requests categories. Is this approach good? bad? Should I do this or stick with db query each time?</p>