yield from Keyword

The yield from keyword is used to delegate the iteration control of one generator to another. It was introduced in PHP 7.0 as part of the generator delegation feature.

This keyword allows to create more maintainable and modular code by reusing the code logic of one generator within another.

Generator delegation works with another generators, Traversable objects, and arrays.

<?php

    function inner() {
        yield 1; // key 0
        yield 2; // key 1
        yield 3; // key 2
    }
    function gen() {
        yield 0; // key 0
        yield from inner(); // keys 0-2
        yield 4; // key 1
    }
    // pass false as second parameter to get an array [0, 1, 2, 3, 4]
    var_dump(iterator_to_array(gen()));

?>

Documentation

See also How to Use Yield and Generators in PHP.

Related : Yield, Generator, Traversable, Array, Jump, range(), Stealth Generator

Added in PHP 7.0