1

How to solve this error. "fatal error maximum function nesting level of '100' reached aborting"

At the moment I will fix it by removing "zend_extension = "/wamp/bin/php/php5.3.8/zend_ext/php_xdebug-2.1.2-5.3-vc9.dll" in php.ini file.

But there will be a problem with Hosting.

Is their any Alternatives for recursive functions with same efficiency ? :)

function MyFunction($pid)
{
echo "task..";
    $obj = MainObj->method($pid);

    if($obj)
    {
      foreach($obj as $val)
      {
         MyFunction($val->pid);
      }
    }
}

Please help me.. :)

1
  • 3
    Could you provide more information about what your function is doing? It would be easier to help if we can see what your expected inputs are and what you are trying to accomplish.
    – seanmk
    Aug 8, 2013 at 17:18

2 Answers 2

2

You can use a stack and skip recursion. If you add the child elements in reverse order to the stack, then you should get the same order as your recursive version. If order doesn't really matter, then there's no need to reverse the iteration.

You don't really have to understand the meaning of the output (as it has none), but just focus on rewriting your recurse function into an iter function.

<?php

function getObj($n)
{
  return $n ? new Foo($n) : null;
}

class Foo implements IteratorAggregate
{
  public $n;

  public function __construct($n)
  {
    $this->n = $n;
  }

  public function getIterator()
  {
    $values = [];

    for ($i = 0; $i < $this->n; ++$i)
    {
      $values[] = (int) ($i / 2);
    }

    return new ArrayIterator($values);
  }
}

function recurse($n)
{
  $obj = getObj($n);

  if ($obj)
  {
    echo "n => ", $obj->n, "\n";
    foreach ($obj as $val)
    {
      recurse($val);
    }
  }
}

function iter($n)
{
  $stack = [];

  $obj = getObj($n);
  if ($obj)
  {
    $stack[] = $obj;
  }

  while ($stack)
  {
    $obj = array_pop($stack);

    echo "n => ", $obj->n, "\n";

    foreach (array_reverse(iterator_to_array($obj)) as $val)
    {
      $obj = getObj($val);
      if ($obj)
      {
        $stack[] = $obj; 
      }
    }
  }
}


recurse(10);
echo "-----\n";
iter(10);

Note that they give the same output, although if getObj() has any side-effects, things may be different between the two.

1

You can increase the nesting limit:

ini_set('xdebug.max_nesting_level', 1000);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.