L.S.,
I just noticed something which seems odd to me: PHP 8.1 deprecated
declaring a function to return by reference when the return type is void:
function &foo() : void {} results in:
"Deprecated: foo(): Returning by reference from a void function is
deprecated"
However, declaring a function to return by reference when the return
type is "never" does not yield either an error or a deprecation notice:
https://3v4l.org/DWs7t
I might well be missing something, but this feels a bit strange and
inconsistent to me.
Should this be fixed by also deprecating return by reference for "never"
functions ?
Smile,
Juliette
Le 9 juil. 2025 à 02:47, Juliette Reinders Folmer php-internals_nospam@adviesenzo.nl a écrit :
L.S.,
I just noticed something which seems odd to me: PHP 8.1 deprecated declaring a function to return by reference when the return type is void:
function &foo() : void {}results in:
"Deprecated: foo(): Returning by reference from a void function is deprecated"However, declaring a function to return by reference when the return type is "never" does not yield either an error or a deprecation notice: https://3v4l.org/DWs7t
I might well be missing something, but this feels a bit strange and inconsistent to me.
Should this be fixed by also deprecating return by reference for "never" functions ?
Smile,
Juliette
Hi Juliette,
The two cases are subtly different, and this can be illustrated by the following example:
Suppose you have an interface mandating to implement a method that returns something by reference.
When you implement that interface, you must provide a method that returns by reference, but you are free to actually never return from it—restricting the return type to never.
On the other hand, you cannot implement it with a method that doesn’t provide a value when returning—changing the return type to void.
In other words, although the combination of “by reference” and “never” is not useful in isolation, it makes nevertheless sense when you think of “never” as a special case of “a value that satisfies a set of constraints”.
—Claude