The Uncallable Function¶
<?php
function foo() {
echo __METHOD__;
yield 1;
}
foo(); // displays nothing, no code is executed
echo 'the end'; // displays the end
?>
The function foo() cannot be called. When the code does so, nothing happens.
The trick is the inclusion of a call to yield (or yield from) in the function. It turns the function into a generator, and calling it directly does nothing. It needs to be called with foreach() or iterator_to_array().
This trick applies to functions, arrow functions, closures and methods.
See Also¶
PHP Features¶
Last updated: 14 July 2026