title: "Binding" variables by reference for delayed processing in class
body: <p>I have a singleton of the following class. What I'm trying to achieve is collect references to various database result arrays that are called throughout the application, then finally make a single request to the database and fetch some replace values (translations) instead of doing it for every single query.</p>
  
  <p>Leaving the pros and cons of this approach aside, what I can't do is the actual replacement of the "original" values (database results) that get passed to the <code>translate()</code> method by reference each time a query is sent to the DB server.</p>
  
  <pre><code>class Translator {
  
      public $translatableTables = ['content' =&gt; ['title', 'body']];
      private $table = 'translations';
      private $queue = array();
  
      public function translate($table, &amp;$results)
      {
          if(!isset($this-&gt;queue[$table])) {
              $this-&gt;queue[$table] = array();
          }
          $this-&gt;queue[$table][] =&amp; $results;
      }
  
      public function fetchTranslations()
      {
          foreach($this-&gt;queue as &amp;$table) {
              foreach($table as &amp;$results) {
                  foreach($results as &amp;$result) {
                      $result-&gt;content = 'DON\'T PANIC THIS IS A TEST';
                  }
              }
          }
          var_dump($this-&gt;queue);
          // dumps just the right data, so I know it's all been collected
      }
  
  }
  </code></pre>
  
  <p>Near the end of the request lifecycle (but before the output is generated) I'm calling the <code>fetchTranslations()</code> method on that same singleton instance.
  What I expect to happen is the original values to be replaced with the string <code>DON'T PANIC THIS IS A TEST</code> but that doesn't work.</p>
  
  <p>Any thoughts/ideas?</p>
  
  <p>Edit: The 3 nested <code>foreach</code> loops may look bad, but that's a short version of the actual business logic in that method which is irrelevant to the problem and skipped for readability.</p>
  
  <p>Edit: As it turned out I was supposed to get each <code>foreach</code> value as reference. That was a real issue with the code but after making said changes it still shows the same behavior (or to be more precise doesn't show the expected behavior).</p>