Friday, 15 July 2011

recursion - Recursive function in php storing results without using static -



recursion - Recursive function in php storing results without using static -

i wrote next recursive function maintain looping through result looking $result->pages->next , calling out curl fetching next page , aggregating results. returns results single object.

private function pager($result) { static $all_results; if(isset($result->pages->next) && !empty($result->pages->next)) { $all_results[] = $this->get_curl_request($result->pages->next); $this->pager(end($all_results)); } else { homecoming $all_results; } }

however don't using static , feels poorly implemented , source of technical debt. more elegant way this?

update

being called with:

return $this->pager($this->get_curl_request("https://api/request/here"));

open changing how called.

try putting $all_result sec parameter , add together homecoming line: $this->pager(end($all_results), $all_results);

code

private function pager($result, $all_results) { if(isset($result->pages->next) && !empty($result->pages->next)) { $all_results[] = $this->get_curl_request($result->pages->next); homecoming $this->pager(end($all_results), $all_results); } else { homecoming $all_results; } }

the above code function homecoming lastly updated array $all_results.

example of use:

$pager_array = $this->pager($result, array());

php recursion static

No comments:

Post a Comment