Do not ignore filtered result in php_filter_validate_url
[hiphop-php.git] / hphp / doc / extension.yield
blob3c2c546d43917e76a04f626020233a0e8a944da2
2 <h2>yield and generator</h2>
4 This feature requires the [[index.php?file=options.compiler | compiler option]]
5 EnableHipHopSyntax=true, or interpreter option Eval.EnableHipHopSyntax=true.
7 HipHop extends PHP to include Python and C#-style generators. If you're
8 unfamiliar with the concept, see
9 [[http://docs.python.org/tutorial/classes.html#generators | the Python docs]].
10 As in Python, the <i>yield</i> keyword marks the enclosing function as a
11 generator:
13   function foo() {
14     $a = 123;
15     yield $a;
16     $a = 456;
17     yield $a;
18   }
20   foreach (foo() as $a) {
21     print "$a,";
22   }
24 The above program outputs "123,456,". To abort a generator sequence, use "yield
25 break".
27   function bar() {
28     $a = 123;
29     // this will stop the "foreach" immediately without any value returned
30     if ($abort) yield break;
31     yield $a;
32     $a = 456;
33     yield $a;
34   }
36 Generators must observe the following restrictions:
38 - Generators are <b>not recursive</b>. In the above example, foo() cannot call
39   foo() while iterating.
40 - Generators are <b>called once</b>: foo() cannot be called again after it's
41   done iterating.
42 - Do not call the rewind() method of the objects (of class Iterator) returned by
43   iterator functions.
45 Also, yield in HipHop also supports passing a value from outside of the
46 generator.
48   function foo() {
49     $a = yield 5;
50     yield $a + 1;
51   }
53 From outside the generator, instead of resuming the generator with
54 Continuation::next(), one can call Continuation::send() to pass a value, which
55 will be assigned to $a, back into the generator.
57 Note that the yield expression in the above example is not really an expression;
58 it can only appear on its own on the RHS of an assignment statement. This is to
59 avoid the complicated evaluation order problem in bizarre expressions like
60 "($a = yield 5) * (yield $a + 3) - ($a = yield 4)".