Hi
I think this is a great idea, and that it should be implemented.
On 17/02/2019 16:36, Rowan Collins wrote:
Instead, ": void" in PHP represents a *behavioural constraint* on the
function - it declares that within this function, anything of the form
"return $value" is to be treated as an error. In other languages, the
same constraint might be enforced by declaring the routine as a
"procedure" rather than a "function".
As such, it is more appropriate to compare "void vs non-void" to "static
vs non-static", which is an invariant constraint - you can't over-ride a
static method with a non-static one, or vice versa.
I think this is flawed reasoning.
Yes, you can view it as behavioral constraint on the current method, but
I do not think that alone is a valid reason to impose the same
constraint on all decendants of that class.
Yes, "static" is an invaraint constraint, but I think we should consider
this "void" constraint differently and not enforce that same invariance.
The reason is that is fits very well with the Liskov substitution
principle. Any decendant class can be subtituted safely in existing
code, even if the decendant should be trying to return something.
This also gives the least amount of surprise to a developer after RFC
https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters
has been implemented. Then developers will expect that return values can
be different as long as the Liskov substitution principle holds.
Terje Bråten.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php