Generator::key
(PHP 5 >= 5.5.0, PHP 7)
Generator::key — Get the yielded key
Список параметров
У этой функции нет параметров.
Возвращаемые значения
Returns the yielded key.
Примеры
Пример #1 Generator::key() example
<?php
function Gen()
{
yield 'key' => 'value';
}
$gen = Gen();
echo "{$gen->key()} => {$gen->current()}";
Результат выполнения данного примера:
key => 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
Коментарии
This is important when considering how other Generators work such as JavaScript's an Python's. While PHP's generator has the ->valid() method they don't, or an equivalent. JS uses Iterator protocol which says next() should return an object of
{
done: bool,
value: mixed
}
In which case you can use keys->done to see if the generator can still be iterated.