PHP 8.1 introduces never
return type, what is it? and what is the difference between never
and void
?
Definition and Usage. The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.
A void return type has been introduced. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.
The PHP team announced the final PHP 8.1 release candidate (RC6) yesterday, with the GA release two weeks away! PHP 8.1 Release Candidate 6 is ready!
never
type declaration introduced to be used as a return type hint for functions that never have return
statement neither implicit nor explicit. And must be terminated by throwing an exception or exiting using exit/die
functions.
function redirect(string $uri): never
{
header('Location: ' . $uri);
exit();
}
Here redirect
is called a never-returning function, because:
1) It has no return
statement defined explicitly.
function redirect(string $uri): never
{
exit();
return 'something';
}
Will prodcue:
PHP Fatal error: A never-returning function must not return
2) It has no return
statement defined implicitly.
function redirect(string $uri): never
{
if (false) {
header('Location: ' . $uri);
exit();
}
}
Since the condition here is never satisfied, the execution jump over the if statement returning an implicit NULL
which will result in:
PHP Fatal error: Uncaught TypeError: redirect(): never-returning function must not implicitly return
3) It ends it's execution with an exit
function
void
can have return;
but never
can't.never
enforces that a function throws or is terminated with exit/die but void
does
not.never
is a subtype of every other type in PHP’s type system, including void (this allows return type covariance).void
over never
and vice versa?You should declare a function return type void
when you expect PHP to execute the next statement after the function call. And you should declare it never
when you don't expect PHP to execute the next statement after that function call.
With never
(PHP 8.1) you can safeguard a function for that it never returns, e.g. if you want to ensure that an endless loop created would really be endless (from the perspective of the call site):
<?php
function eternity(string $forEternity): never
{
start:
usleep(1000);
goto start;
}
eternity('the future');
// <- we are here only after the next big-bang, not in this current universe.
This code is super-simplified, normally forgetting the goto statement here would trigger the error.
Endless loops are normally also not necessary to guard for (as normally we never want them to happen), but never
is useful for a function to die()
, exit()
or throw
an Exception, always.
Again, although these three ways to leave a function are not recommended in standard control flow (the first two are even controlling the running process in some SAPIs). But finally (no pun intended), it is possible to have the IDE and tooling see that a function never returns right from the return type information encoded in the code. This then also includes humans who are using such tools or who are reading the code.
With void
(PHP 7.1) you safeguard that a function never returns a type, including the bottom type never
(as you can only use void
or never
).
<?php
function back_from_eternity(string $forEternity): void
{
usleep(100000); // not so long
return $forEternity
}
$echo = back_from_eternity('soon');
Allows PHP to prevent the echo back from eternity as well as the reader to spot that the $echo =
(and whatever is done afterwards with that variable) still needs the usual maintenance / debugging / testing or development.
Both have the benefit that they throw when they are violated so the program will never ever continue as-is and render the function call returns void (all puns intended).
Any side-effects from calling the function should still be considered. It's normally about them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With