1

i'm trying to get a recursive function working in PHP but it fails with a "Fatal error: Allowed memory size of 134217728 bytes exhausted".

Why isn't this working?

    $atest = array();
    $atest = $this->resolveCategories(3,$atest);
    var_dump($atest);

And the recursive function:

 private function resolveCategories($iCategoryId,$aCategories){
    $oCategory = CategoryQuery::create()->findOneById($iCategoryId);
    if ($oCategory->getParentId() != null){
        array_push($aCategories,$oCategory->getName());
        $this->resolveCategories($iCategoryId,$aCategories);
    }
    return $aCategories;
}
7
  • Are you asking for how to lift the memory limit ? or how to improve your algorithm ? Commented Jan 14, 2014 at 19:26
  • :) No i think the algorithm should determine fast because i only have a few entries in my database. So i think i got an coding error somewhere which i don't find. Commented Jan 14, 2014 at 19:28
  • It seems you have possibly triggered an infinite recursion somehow. It is hard to debug this without seeing the actual data being used. Have you treid to debug? Can you identify where the values differ from what is expected? Commented Jan 14, 2014 at 19:28
  • what does parent ID return? it seems to always return a value that is not null Commented Jan 14, 2014 at 19:29
  • 1
    How does your function know when to stop? You are just calling the same function with the same parameters over and over. You created an infinite loop. $this->resolveCategories($iCategoryId,$aCategories); Perhaps you wanted to do $this->resolveCategories($iCategoryId-1,$aCategories); or something? Commented Jan 14, 2014 at 19:32

4 Answers 4

2

I think you meant to call

$this->resolveCategories($oCategory->getParentId(), $aCategories);

inside, not

$this->resolveCategories($iCategoryId, $aCategories);
Sign up to request clarification or add additional context in comments.

1 Comment

Also, the whole polish notation thing is a bit old. Try meaningful name. $category and $categoryNames reads a lot better.
1

How does your function know when to stop? You are just calling the same function with the same parameters over and over. You created an infinite loop.

private function resolveCategories($iCategoryId,$aCategories){
    // code...
    $this->resolveCategories($iCategoryId,$aCategories);
    // code...
}

You are never changing the $iCategoryId parameter, so you just keep getting the same row (which happens to have a parent) over and over and over and over.

Perhaps you wanted:

$this->resolveCategories($iCategoryId-1, $aCategories);

or something?

Comments

0

For recursive functions , you always need a break statement which is generally a IF statement that will return false in on iteration. Generaly you can use a $depth variable and determinate how deep your recursive function can go!

2 Comments

i tried an else-part in my if clause. The break point should be if the actual element has no parentId
getParentId() is supposed to return object right ? If so use is_object function and do not compare with Null
0

In general I don't understand how your algorithm is supposed to work. It seems like your array is going to recursively grow in size as you continue to pass the original array (plus item pushed to it to the recursive call.

1 Comment

The algorithm is supposed to add all category names to a array. it should end if the actual element has no more parents (parentId == null). Then it should return the generated array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.