hiphop-php /hphp/test/quick/generator_vars.php

Language PHP Lines 98
MD5 Hash 159dc22173482d92f4fc50ea462578eb Estimated Cost $2,080 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

class logger {
  static $x = 0;
  private $idx;
  function __construct() {
    $this->idx = self::$x++;
    printf("logger %d constructing\n", $this->idx);
  }
  function __destruct() {
    printf("logger %d destructing\n", $this->idx);
  }
}

function create() {
  $x = 5;
  yield $x;
  $s = 'foo';
  yield $s;
  $$s = 1234;
  $z = $$s;
  yield $z;
  yield $$s;
}

function unusedarg($x, $y) {
  $z = 5;
  yield compact('x', 'z');
  $s = 'foo';
  yield 'almost there';
  $$s = 'inside foo';
  yield compact('foo', 's');
  yield compact('x', 'y', 'foo', 'z');
}

function dumpgen($g) {
  foreach ($g as $v) {
    var_dump($v);
  }
}

function getargs($foo) {
  yield 0xdeadbeef;
  yield func_get_args();
  yield func_get_arg(3);
}

function genthrow() {
  throw new Exception();
  yield 5;
}

function manylocals() {
  $a = 1;
  $b = 2;
  $c = 3;
  $d = 4;
  $e = 5;
  $f = 6;
  $g = 7;
  $h = 8;
  $i = 9;
  $j = 10;
  $k = 11;
  $l = 12;
  $a = yield get_defined_vars();
  $b = 0xdeadbeef;
  $c = yield get_defined_vars();
  $d = $e = 0xba53b411;
  yield get_defined_vars();
}

function main() {
  dumpgen(create());
  dumpgen(unusedarg(new logger(), 5));
  dumpgen(getargs(1, 2, 3, 4, 5));
  $g = genthrow();
  try {
    $g->next();
  } catch (Exception $e) {}
  try {
    $g->next();
  } catch (Exception $e) {
    var_dump($e->getMessage());
  }

  $g = manylocals();
  var_dump($g->current());
  $g->send(new stdclass);
  var_dump($g->current());
  $g->send($g);
  var_dump($g->current());
  $g->next();
  var_dump($g->current());
  var_dump($g->valid());
}
main();
Back to Top