Beispiel #1
0
function gen2()
{
    yield from gen1();
}
<?php

function gen1()
{
    return 42;
    (yield 24);
}
$gen = gen1();
// Calling getReturn() directly here is okay due to auto-priming
var_dump($gen->getReturn());
function gen2()
{
    (yield 24);
    return 42;
}
$gen = gen2();
var_dump($gen->current());
$gen->next();
var_dump($gen->getReturn());
// & for generators specifies by-reference yield, not return
// so it's okay to return a literal
function &gen3()
{
    $var = 24;
    (yield $var);
    return 42;
}
$gen = gen3();
var_dump($gen->current());
$gen->next();
var_dump($gen->getReturn());
Beispiel #3
0
<?php

function gen1()
{
    (yield 1 => 2);
    (yield 2 => 3);
}
var_dump(gen1()->valid());
var_dump(gen1()->current());
var_dump(gen1()->key());
var_dump(gen1()->next());
function gen2()
{
    if (false) {
        (yield 1 => 2);
    }
}
var_dump(gen2()->valid());
var_dump(gen2()->current());
var_dump(gen2()->key());
try {
    var_dump(gen2()->next());
} catch (Exception $e) {
    var_dump($e->getMessage());
}
<?php

function gen1()
{
    (yield 'a');
    (yield 'aa');
}
function gen2()
{
    (yield 'b');
    (yield 'bb');
}
$it = new MultipleIterator();
$it->attachIterator(gen1());
$it->attachIterator(gen2());
foreach ($it as $values) {
    var_dump($values);
}
Beispiel #5
0
<?php

if (false) {
    function gen1()
    {
        (yield 1);
    }
}
if (true) {
    function gen1()
    {
        (yield 2);
    }
    function gen2()
    {
        (yield 3);
    }
}
if (false) {
    function gen2()
    {
        (yield 4);
    }
}
foreach (gen1() as $x) {
    var_dump($x);
}
foreach (gen2() as $x) {
    var_dump($x);
}