I have this code
$return = $ep->$method($params);
if ($return === null) {
throw new Exception('Endpoint has no return value');
}
return $return;
Is there any way to distinguish between a method that returns null
and a method that does not return anything?
It's not possible. When no return value is set the function automatically returns null.
If the function doesn't return anything, then you should not test it's return value. You should know which functions are expected to return something or nothing at all - even if you're not the one who wrote them.
With PHP7’s return type declaration feature:
function a(): void {
return null; // :(
}
function b(): void {
// :)
}
function c(): void {
return; // :)
}
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