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

Type de retour non appliqué

<?php

function foo() : Generator {
    yield 1;

    return 'not a generator';
}

foreach($g = foo() as $b) {
    print $b;  // print 1
}

echo $g->getReturn(); // print 'not a generator'

?>

Un type de retour sur une méthode signifie que la méthode doit retourner quelque chose de ce type. C’est vrai, sauf pour les générateurs. Ces méthodes contiennent yield ou yield from, et doivent utiliser le type de retour Generator. Dans ce cas, le type de retour effectif n’est pas du tout vérifié.

See Also

PHP Features

Last updated: 11 October 2026