News for hackers. Stuff that matters.
Re:Upcoming changes in PHP 7.1 by arenaninja on Monday May 23, 2016 @08:18AM (#11753343)
What is the reasoning for type hinting void when the PHP manual specifies that a NULL value is returned if the return is omitted? http://php.net/manual/en/functions.returning-values.php
Re:Upcoming changes in PHP 7.1 by blowski on Monday May 23, 2016 @08:26AM (#11753378)
Self-documenting code. I'm already using return types wherever I can, and it's nice to keep things consistent.

Also, it helps prevent the abuse of interfaces. I can say "this method does not return values" and enforce it. That can be useful on a big project, with junior devs.

Re:Upcoming changes in PHP 7.1 by arenaninja on Monday May 23, 2016 @08:34AM (#11753421)
I was talking more about the inconsistency between return type void when it returns null. I'm asking whether this behavior has changed and functions without a return will now return void or ???. It's a pretty big inconsistency IMHO.
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @08:55AM (#11753523)
Functions with a `: void` return type will still give you a NULL if you use them in an expression. I didn't want to break the property of existing PHP functions that they can always be used as expressions. I reasoned that your IDE or whatever can always warn you if you mistakenly try to use the result of such a function.

PHP's built-in functions documented as "void" also return NULL, so I suppose there's an argument from consistency.

Re:Upcoming changes in PHP 7.1 by blowski on Monday May 23, 2016 @08:40AM (#11753450)
It's a good point, and one which I personally hadn't thought about. The RFC specifically mentions it, so it's obviously a concern:

> Choosing one over the other suggests intent. If you specify a value, it suggests the value is significant. In a void function, the return value is insignificant: it's always the same and has no actual usefulness. https://wiki.php.net/rfc/void_return_type#why_isn_t_return_n...

I guess this 'pragmatism over consistency' fits in with the general ethos of PHP. I guess it's why so many people hate it, but I can't imagine a situation in which this will actually cause a problem.

Re:Upcoming changes in PHP 7.1 by spdionis on Monday May 23, 2016 @08:57AM (#11753531)
Even if you have a point it wouldn't matter because making void functions return something else than null would be a big (useless) BC break.

There's a lot of code that uses "return;" to mean "return null;" and honestly I don't see it as a big problem.

Re:Upcoming changes in PHP 7.1 by arenaninja on Monday May 23, 2016 @10:30AM (#11754051)
The point I was trying to make is that PHP does not have -- nor should it have, I think -- a concept of void. Functions without a return statement already return null. "return;" is already equivalent to "return null;" as well, as you pointed out. It would be more consistent to type-hint return null instead of introducing a new return type (void) into the language. I know PHPDoc already uses "@return void", and I have never liked it for the same reason. It is not a concept used anywhere else in PHP, AFAIK.
Re:Upcoming changes in PHP 7.1 by wtetzner on Monday May 23, 2016 @12:35PM (#11754925)
I think having a void return type makes a lot of sense, because it says "this function will never return anything but null".
Re:Upcoming changes in PHP 7.1 by tokenizerrr on Monday May 23, 2016 @08:42AM (#11753457)
This is the same as other languages. In for example Java you have `public void method()`, and you then `return` which results in a `null`
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @08:43AM (#11753463)
Are you sure about that? I don't think you can use the results of void functions in Java as expressions.
Re:Upcoming changes in PHP 7.1 by duskwuff on Monday May 23, 2016 @01:37PM (#11755413)
This is completely incorrect. Java's null value is only valid as a value of an object type (e.g, Object x = null), and void functions don't return an object.
Re:Upcoming changes in PHP 7.1 by tokenizerrr on Monday May 23, 2016 @03:36PM (#11756278)
The compiler doesn't allow it, but if you call the function with for example reflection you get null.
Re:Upcoming changes in PHP 7.1 by elgenie on Wednesday May 25, 2016 @09:47AM (#11769700)
You want to be able to communicate intent to clients of the function, especially with static tooling to check for common errors. For example:

https://github.com/facebook/hhvm/blob/HHVM-3.13/hphp/hack/te...

... has an obvious code smell detectable statically:

https://github.com/facebook/hhvm/blob/HHVM-3.13/hphp/hack/te...