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

No Returntype Enforced

<?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'

?>

A return type on a method means that the method must return something of that type. This is true, unless for generators. Such methods contains yield or yield from, and must use the Generator returntype. Then, the actual return type is not checked, at all.

See Also

PHP Features

Last updated: 10 September 2026