Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

N’oubliez pas yield

<?php

function foo() {
    yield 'foo';
    goo();
}

function too() {
    yield 'too';
    yield from goo();
}

function hoo() {
    yield 'hoo';
    yield goo();
}

function goo() {
    yield 'goo';
}

foreach(foo() as $a) { print $a.PHP_EOL;}
// goo
foreach(too() as $a) { print $a.PHP_EOL;}
// too goo
foreach(hoo() as $a) { print $a.PHP_EOL;}
// hoo Uncaught Error: Object of class Generator
// could not be converted to string

?>

Il est possible de déléguer un générateur à un autre générateur.

Un point à garder à l’esprit est qu’ils ne doivent pas être appelés directement, car rien ne se passe.

Et n’oubliez pas la partie from du mot-clé, sinon cela produit le générateur au lieu de l’exécuter.

See Also

PHP Features

Last updated: 11 October 2026