Never return type
Some functions or methods will never return anything because they do things that will halt execution. Examples of this type of function or method could be methods or functions that throw an exception
, or maybe they literally call die
or exit
to fully halt all execution.
It is worth having a read through the RFC, which is based on the original noreturn type nomenclature:
PHP: rfc:noreturn_type
https://wiki.php.net/rfc/noreturn_type
While these kinds of methods are probably quite rare, it is nice to know that we can now properly assign a return type and that can also be enforced by interfaces and inheritance rules to ensure a consistent API and behavior.
Some scenarios where you might use a never return type are as follows:
- A
throwException
method that simply throws an exception and so can never return. - An
httpRedirect
method that issues a 301 HTTP header and then ends execution with exit. - A
loop
method that will loop continuously until...