send

Generator::send

Send a value to the generator

ENTITY

public

mixed

Generator::send

mixed

value

Sends the given value to the generator as the result of the current ENTITY expression and resumes execution of the generator.

If the generator is not at a ENTITY expression when this method is called, it will first be let to advance to the first ENTITY expression before sending the value. As such it is not necessary to "prime" PHP generators with a Generator::next call (like it is done in Python).

ENTITY

value
Value to send into the generator. This value will be the return value of the ENTITY expression the generator is currently at.

ENTITY

Returns the yielded value.

ENTITY

Using Generator::send to inject values

<?php
function printer() {
    echo "I'm printer!".PHP_EOL;
    while (true) {
        $string = yield;
        echo $string.PHP_EOL;
    }
}

$printer = printer();
$printer->send('Hello world!');
$printer->send('Bye world!');
?>

    

ENTITY

I'm printer!
Hello world!
Bye world!

版本号 #2
由 壳壳虫 创建于 2026-10-31 17:44:36 UTC
由 壳壳虫 更新于 2026-10-31 17:44:37 UTC