Generator::current
(PHP 5 >= 5.5.0)
Generator::current — Get the yielded value
Описание
Список параметров
У этой функции нет параметров.
Возвращаемые значения
Returns the yielded value.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник языка
- Встроенные интерфейсы и классы
- Функция Generator::current() - Get the yielded value
- Generator::getReturn
- Функция Generator::key() - Get the yielded key
- Функция Generator::next() - Resume execution of the generator
- Функция Generator::rewind() - Rewind the iterator
- Функция Generator::send() - Send a value to the generator
- Функция Generator::throw() - Throw an exception into the generator
- Функция Generator::valid() - Check if the iterator has been closed
- Функция Generator::__wakeup() - Serialize callback
Коментарии
<?php
function gen_one_to_three() {
for ($i = 1; $i <= 3; $i++) {
// Note that $i is preserved between yields.
yield $i;
}
}
$generator = gen_one_to_three();
foreach ($generator as $value) {
echo "regular : ".$value , PHP_EOL;
echo "With current function : ".$generator->current(),PHP_EOL;
}
?>
current() advances untouched generator, same as next(), it makes the first step/iteration. the following calls will not.
non-yielded value will be NULL
I think what "gib-o-master" said is wrong.
Generator::current method does not advances "untouched generator".
let me give an example:
function y1()
{
yield 1;
}
$g = y1();
if ($g->valid()) { //at this point, PHP outputs "valid"
echo "valid\n";
}
echo "current v:" . $g->current() . "\n"; //at this point, PHP outputs current v:1
if ($g->valid()) { //at this point, PHP still outputs "valid"
echo "valid\n";
} else {
echo "not valid\n";
}
if Generator::current method advances generator, then above If statement should outputs "not valid"