Generator::send

(PHP 5 >= 5.5.0)

Generator::sendSend a value to the generator

Описание

public mixed Generator::send ( mixed $value )

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

If the generator is not at a yield expression when this method is called, it will first be let to advance to the first yield 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).

Список параметров

value

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

Примеры

Пример #1 Using Generator::send() to inject values

<?php
function printer() {
    while (
true) {
        
$string = yield;
        echo 
$string;
    }
}

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

Результат выполнения данного примера:

Hello world!
Bye world!

Возвращаемые значения

Returns the yielded value.

Коментарии

Reading the example, it is a bit difficult to understand what exactly to do with this. The example below is a simple example of what you can do this.

<?php
function nums() {
    for (
$i 0$i 5; ++$i) {
               
//get a value from the caller
       
$cmd = (yield $i);
       
        if(
$cmd == 'stop')
            return;
//exit the function
       
}     
}

$gen nums();
foreach(
$gen as $v)
{
    if(
$v == 3)//we are satisfied
       
$gen->send('stop');
   
    echo 
"{$v}\n";
}

//Output
0
1
2
3
?>
2013-07-17 15:11:41
http://php5.kiev.ua/manual/ru/generator.send.html
<?php
function foo() {
   
$string yield;
    echo 
$string;
    for (
$i 1$i <= 3$i++) {
       
yield $i;
    }
}

$generator foo();
$generator->send('Hello world!');
foreach (
$generator as $value) echo "$value\n";
?>

This code falls with the error:
PHP Fatal error:  Uncaught exception 'Exception' with message 'Cannot rewind a generator that was already run'.
foreach internally calls rewind, you should remember this!
2013-10-08 21:30:38
http://php5.kiev.ua/manual/ru/generator.send.html
an example:

$coroutine=call_user_func(create_function('', <<<'fun_code'
    echo "inner 1:\n";
    $rtn=(yield 'yield1');
    echo 'inner 2:';var_export($rtn);echo "\n";
    $rtn=(yield 'yield2');
    echo 'inner 3:';var_export($rtn);echo "\n";
    $rtn=(yield 'yield3');
    echo 'inner 4:';var_export($rtn);echo "\n";
fun_code
));
echo ":outer 1\n";                                       // :outer 1
var_export($coroutine->current());echo ":outer 2\n";     // inner 1:, 'yield1':outer 2
var_export($coroutine->current());echo ":outer 3\n";     // 'yield1':outer 3
var_export($coroutine->next());echo ":outer 4\n";        // inner 2:NULL, NULL:outer 4
var_export($coroutine->current());echo ":outer 5\n";     // 'yield2':outer 5
var_export($coroutine->send('jack'));echo ":outer 6\n";  // inner 3:'jack', 'yield3':outer 6
var_export($coroutine->current());echo ":outer 7\n";     // 'yield3':outer 7
var_export($coroutine->send('peter'));echo ":outer 8\n"; // inner 4:'peter', NULL:outer 8
2015-09-29 09:47:27
http://php5.kiev.ua/manual/ru/generator.send.html

    Поддержать сайт на родительском проекте КГБ