A never-returning function must not return

Description

When using the never keyword, the function shall not use the return keyword. never means that the function will not return, but rather die, throw an exception, or trigger an error.

Also note that never cannot be used in a union type.

This error message applies to functions, closures and arrow functions. In PHP 8.4, a separate message applies to methods.

Example

<?php

function foo() : never {
     return true;
}

?>

Solutions

  • Remove the return keyword in the body of the function.

  • Change the never return type to another type.