Hi Matt & Ondřej,

One of the things that makes PHP different from other languages (and
for better for a change) is that every function returns a value, even
where there is no explicit return statement.

This eliminates a large number of edge-cases in code like:

  function log_result(mixed $bar) {...}
  log_result($anyCallable());

is always valid in PHP, whereas in other languages similar code can
either refuse to compile, or error out when run, or other things like
'undefined' variables.

I.E. in other languages, there are three exit conditions:
 * no value returned
 * a value returned
 * function has no normal exit

And so other languages need to indicate between 'void' and 'no return'

But in PHP there are only two exit conditions*:
 * a value returned
 * function has no normal exit

I (and others) brought this up during the void RFC:
https://news-web.php.net/php.internals/88990 and said that null was
the right choice over void, as it matches what the language actually
does.

That would have left void available to mean 'this function' does not
exit normally.

Unfortunately people in the community have started doing what I
feared, and using void as 'no value is returned', which is not what
the language actually _does_.

I realise the above might be slightly discombobulating if, for
example, some people had written large static code analyzers that had
misinterpreted void like this.

I think we should introduce null. But we shouldn't introduce
'noreturn' even if a large number have misunderstood what the
behaviour of the language is. It might be appropriate for the other
languages that are listed in the other language section, but it's not
appropriate in PHP which is different to those languages.

cheers
Dan
Ack


* so the usage of null and void return types should be:

function returns(): null {
if (rand(0, 100) === 0) { throw new \Exception('Surprise!');}
// intentionally no code
}

function never_returns(): void {
if (rand(0, 100) === 0) { throw new \Exception('Surprise!');}
while (1) {}
}

For both of them, there are exceptions to the declared return type,
but otherwise 'returns' returns the value null, and 'never_returns'
never returns a value.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to