PHP.mk документација

Generator::getReturn

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

generator.getreturn.php PHP.net прокси Преводот е вчитан
Оригинал на PHP.net
Патека generator.getreturn.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + преведен приказ Кодовите, табелите и белешките остануваат читливи во истиот тек.
Generator::getReturn

Референца за `generator.getreturn.php` со подобрена типографија и навигација.

generator.getreturn.php

Generator::getReturn

Интерфејсот SessionUpdateTimestampHandlerInterface

Generator::getReturn(PHP 7, PHP 8)

= NULL

public Generator::getReturn(): mixed

Параметри

Оваа функција нема параметри.

Вратени вредности

Земи ја вратената вредност на генераторот

Примери

Пример #1 Враќа вратена вредност на генераторот откако ќе заврши со извршување. example

<?php

$gen
= (function() {
yield
1;
yield
2;

return
3;
})();

foreach (
$gen as $val) {
echo
$val, PHP_EOL;
}

echo
$gen->getReturn(), PHP_EOL;

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

1
2
3

Белешки од корисници 3 белешки

Начини за специфицирање на буквални стрингови
пред 8 години
The note by jm at guide42 dot com is inaccurate. Generator::getReturn() does not check the existence of the return statement, but rather whether the function has returned (finished executing).

<?php
function f(){
  yield;
}

f()->getReturn(); // Exception: Cannot get return value of a generator that hasn't returned

$f = f();
$f->next(); // NULL
$f->getReturn(); // NULL, because having no return statement implies an empty `return;`, and the void return value is resolved to null.
Хејли Вотсон
пред 7 години
Just to be clear, the value returned by a generator is not yielded while it is running, but is available afterwards through the getReturn() method.

One use for this could be to record the output of a generator so that it can be iterated over again without having to reconstruct and re-run the generator. (Indeed, one could write a generator that simply does exactly this for any given iterable.)

<?php
function generate_squares($n)
{
    $record = [];
    for($i = 0; $i <= $n; ++$i)
    {
        yield ($record[] = $i * $i);
    }
    return $record;
}

$squares = generate_squares(10);
foreach($squares as $s)
{
    echo $s, ' ';
}
$recorded = $squares->getReturn();
echo "\nThat's [", join(', ', $recorded), "]";
?>

Recording keys as well would take a bit more work, as generators are free to repeat them but arrays aren't.
Generator::getReturn()
пред 8 години
I'm not quite sure -- I feel there is an ambiguity issue here with returning a value inside a generator and ->getReturn() method of such generator as I have been able to return an array of sorts, something like this:

function dequeue(){
try{
  foreach($this->buffer->data as $data){ ... }
}
  return $statistics;
}

foreach(($generator = $this->dequeue()) as $bdata){
.....
}

if($statistics = $generator->getReturn()){
 // generator returns $statistics
}

Obviously this is not a complete example, but $statistics does become available with the returned array.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.