title: Recursive generators in PHP
body: <h1>Introduction</h1>
  
  <p>Since version 5.5 in PHP there's such great thing as <a href="http://php.net/manual/en/language.generators.overview.php" rel="noreferrer">generators</a>. I will not repeat official manual page, but they are great thing for short definition of iterators. The most-known sample is:</p>
  
  <pre><code>function xrange($from, $till, $step)
  {
     if ($from&gt;$till || $step&lt;=0)
     {
        throw new InvalidArgumentException('Invalid range initializers');
     }
  
     for ($i = $from; $i &lt; $till; $i += $step)
     {
        yield $i;
     }
  }
  
  //...
  
  foreach (xrange(2, 13, 3) as $i)
  {
     echo($i.PHP_EOL); // 2,5,8,11
  }
  </code></pre>
  
  <p>and generator is actually not a function, but an instance of a concrete class:</p>
  
  <pre><code>get_class(xrange(1, 10, 1)); // Generator
  </code></pre>
  
  <p><br/></p>
  
  <h1>The problem</h1>
  
  <p>Done with RTM stuff, now moving on to my question. Imagine that we want to create generator of <a href="http://en.wikipedia.org/wiki/Fibonacci_number" rel="noreferrer">Fibonacci numbers</a>. Normally, to get those, we can use simple function:</p>
  
  <pre><code>function fibonacci($n)
  {
     if(!is_int($n) || $n&lt;0)
     {
        throw new InvalidArgumentException('Invalid sequence limit');
     }
     return $n &lt; 2 ? $n : fibonacci($n-1) + fibonacci($n-2);
  }
  
  var_dump(fibonacci(6)); // 8
  </code></pre>
  
  <p>Let's transform this into something, that holds <em>sequence</em> and not only it's last member:</p>
  
  <pre><code>function fibonacci($n)
  {
     if (!is_int($n) || $n&lt;0)
     {
        throw new InvalidArgumentException('Invalid sequence limit');
     }
     if ($n&lt;2)
     {
        return range(0, $n);
     }
     $n1 = fibonacci($n-1);
     $n2 = fibonacci($n-2);
     return array_merge($n1, [array_pop($n1)+array_pop($n2)]);
  }
  
  //...
  
  foreach (fibonacci(6) as $i)
  {
     echo($i.PHP_EOL); // 0,1,1,2,3,5,8
  }
  </code></pre>
  
  <p>We have now a function that returns array with full sequence</p>
  
  <p><br/></p>
  
  <h1>The question</h1>
  
  <p>Finally, the question part: how can I transform my latest <code>fibonacci</code> function so it will <strong>yield</strong> my values, not holding them in an array? My <code>$n</code> can be big, so I want to use benefits of generators, like in <code>xrange</code> sample. Pseudo-code will be:</p>
  
  <pre><code>function fibonacci($n)
  {
     if (!is_int($n) || $n&lt;0)
     {
        throw new InvalidArgumentException('Invalid sequence limit');
     }
  
     if ($n&lt;2)
     {
        yield $n;
     }
  
     yield fibonacci($n-2) + fibonacci($n-1);
  }
  </code></pre>
  
  <p>But this, obviously, is crap since we can't handle with it like this way because recursion will cause object of class <code>Generator</code> and not <code>int</code> value.</p>
  
  <p><em>Bonus</em>: getting fibonacci sequence is just a sample for more general question: how to use generators with recursion in common case? Of course, I can use standard <a href="http://php.net/manual/en/class.iterator.php" rel="noreferrer">Iterator</a> for that or re-write my function to avoid recursion. But I want to achieve that with generators. Is this possible? Does this worth efforts to use this such way?</p>