title: Caching data with class properties - why is it a bad idea?
body: <p>I've recently been reading many articles on scalability for PHP apps. Nearly all of the articles I have read have mentioned caching, so I came up with this idea of caching DB data in class properties, to prevent excess DB queries. I wanted to share the idea so I blogged about it, only to have my teacher tell me it was pointless and silly. Apart from using the words pointless and silly, he couldn't really explain why it was bad. Could someone here please explain why this method of caching, to help scale PHP applications is bad?</p>
  
  <p><strong>The method:</strong></p>
  
  <p>Theory:</p>
  
  <p>Instead of fetching the data in every method (where needed) from the DB, executing query after query, I thought it would be a good idea to have a class property (variable) that stores the fetched DB data, to prevent the need of duplicate queries or queries that will return the same data.</p>
  
  <p>If you didn't get that, here's an example taken from my blog:</p>
  
  <hr>
  
  <p>I'm going to bring Facebook into this example, just to ease the explanation a bit. Let's say we were re-coding the user class for the social network.</p>
  
  <pre><code>class FBuser
  {
  
  }
  </code></pre>
  
  <p>The obvious methods this class would contain:</p>
  
  <pre><code>getStatusUpdates()
  getAccountInfo()
  getFriendIDs()
  </code></pre>
  
  <p>Originally, those methods would each have to execute database queries, to get the required data. But with the caching method, I would define a class property to store the cached data, and would have all the DB querying going on in a single method:</p>
  
  <pre><code>class FBuser
  {
      private $userCache = array();
  
      private function getData( $dataToGet = '' )
      {
  
      //all of my db querying would happen here
  
      }
  
  }
  </code></pre>
  
  <p>But in that same method, I would also be looking for cache, if I'm allowed to do so:</p>
  
  <pre><code>private function getData( $dataToGet = '' , $useCache = true )
  {
     //am I allowed to use cache?
     if ( $useCache === true )
     { 
         //does the appropriate data exist in cache?
         if ( isset($this-&gt;userCache[ $dataToGet ]) )
         {
             return $this-&gt;userCache[ $dataToGet ];//return the cached data, and forget about the DB queries
         }
  
     }
  
     //if we get here, caching was disabled or the required data has not yet been cached :(
     //all of my db querying would happen here
  
     //store the data that's just been fetched by the queries in the cache property
  
  }
  </code></pre>
  
  <p>This way, I could call <code>getData( 'the data I want' , true );</code> whenever I want to fetch data from the DB, allowing me to use the cached data where and when possible. </p>
  
  <p>So if I ever needed to call <code>getAccountInfo()</code>, <code>getStatusUpdates()</code> or <code>getFriendIDs()</code> multiple times, this method would prevent multiple DB queries being executed = good for scaling (I would think).</p>
  
  <hr>