3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class X { function __construct($id, $gen) { $this->id = $id; $this->gen = $gen; } function __destruct() { echo "__destruct $this->id\n"; var_dump($this->gen->current()); var_dump($this->gen->key()); echo "\n\n"; } } function gen() { $self = yield (0); var_dump($self->current()); echo "After yield 0\n"; yield new X('key', $self) => new X('value', $self); echo "After yield X\n"; return 12; } $a = gen(); var_dump($a->current()); $a->send($a); var_dump($a->current()); $a->next(); var_dump($a->getReturn());
based on lJjVg
Output for 7.0.1 - 7.2.0
int(0) int(0) After yield 0 object(X)#3 (2) { ["id"]=> string(5) "value" ["gen"]=> object(Generator)#1 (0) { } } After yield X int(12) __destruct key NULL NULL __destruct value NULL NULL
Output for 7.0.0
int(0) int(0) After yield 0 object(X)#3 (2) { ["id"]=> string(5) "value" ["gen"]=> object(Generator)#1 (0) { } } After yield X __destruct value object(X)#3 (2) { ["id"]=> string(5) "value" ["gen"]=> object(Generator)#1 (0) { } } object(X)#2 (2) { ["id"]=> string(3) "key" ["gen"]=> object(Generator)#1 (0) { } } __destruct key Fatal error: Uncaught Error: Cannot resume an already running generator in /in/tpvPO:7 Stack trace: #0 /in/tpvPO(7): Generator->current() #1 /in/tpvPO(19): X->__destruct() #2 [internal function]: gen() #3 /in/tpvPO(26): Generator->next() #4 {main} thrown in /in/tpvPO on line 7
Process exited with code 255.
Output for 5.5.24 - 5.6.28
Parse error: syntax error, unexpected '(' in /in/tpvPO on line 14
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected 'new' (T_NEW) in /in/tpvPO on line 17
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected T_NEW in /in/tpvPO on line 17
Process exited with code 255.