title: Recursive function in php storing results without using static
body: <p>I wrote the following recursive function to keep looping through result looking for <code>$result-&gt;pages-&gt;next</code> and calling out to curl fetching the next page and aggregating the results. Finally it returns all results as a single object.</p>
  
  <pre><code>     private function pager($result) {
          static $all_results;
  
          if(isset($result-&gt;pages-&gt;next) &amp;&amp; !empty($result-&gt;pages-&gt;next)) {
              $all_results[] = $this-&gt;get_curl_request($result-&gt;pages-&gt;next);
              $this-&gt;pager(end($all_results));
          } else {
              return $all_results;
          }
      }
  </code></pre>
  
  <p>However I really don't like using <code>static</code> and it feels poorly implemented and a source of technical debt. What is a more elegant way to do this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Being called with:</p>
  
  <pre><code>return $this-&gt;pager($this-&gt;get_curl_request("https://api/request/here"));
  </code></pre>
  
  <p><em>Open to changing how it is called.</em></p>