News for hackers. Stuff that matters.
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @07:20AM (#11753102)
I'm more excited by some features not detailed in the article.

Return type declarations, which were introduced in PHP 7.0, are being enhanced somewhat.

First, it's now possible to declare nullable return types (previously all return types did not permit null):

  public function getFoo(): ?int;
(You can also use ? on parameter and property types.)

Second, we now have a void return type for functions where there is no useful return value:

  public function handle(): void;
(Disclaimer: I wrote the void return type RFC and implementation, so I'm biased here.)

We're also getting type declarations for class properties:

  class Person
  {
      public string $name;
      public int $age;
      public bool $isEmployee;
  }
There's a few other things as well. The list() syntax (destructuring assignment) has been shortened to [] and now lets you specify keys (disclaimer: I was involved in both of these). Also, trying to add non-numeric strings together with + now produces a warning (disclaimer: also me).
Re:Upcoming changes in PHP 7.1 by blowski on Monday May 23, 2016 @08:27AM (#11753382)
> Disclaimer: I wrote the void return type RFC and implementation, so I'm biased here.

I'll take this opportunity to say thank you, for this, and all the other hard work you put into PHP internals.

Re:Upcoming changes in PHP 7.1 by djsumdog on Monday May 23, 2016 @12:55PM (#11755071)
I'll be honest that I was firmly in the /r/lolphp camp for years, but PHP7 shows real promise. There's a real AST parser and the core team seems to be doing a lot to shore up the language.

I still think adding a string to a number should error instead of giving a warning, but one thing at a time. PHP7 and the future roadmap makes it look like PHP is on a path to not turning into Perl.

Re:Upcoming changes in PHP 7.1 by raiph on Monday May 23, 2016 @01:34PM (#11755391)
What do you think of the Perl 6 approach of providing both DWIM (weaker) and DWIS (stronger) typing?

* Numeric and string operators are kept distinct. For example `+` means numeric addition, never concatenation or some other string operation. To compare two strings, use `eq`, to compare two numbers use `==`. Etc. This sets things up nicely for supporting both DWIM and DWIS typing.

* DWIM typing: If you write something like `a + b`, you always get numeric addition. If need be the compiler tries to coerce `a` and `b` to numbers. `"12"` or `" 12" will both successfully coerce to 12. `"123foo"` and `"bar456"` will fail to coerce. If the coercion fails the program crashes at run-time.

* DWIS typing: To tighten type checking, add a type constraint. For example, `my Numeric (\a, \b) = ...` ensures that `a` and `b` only ever contain a numeric value.

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...

Re:Upcoming changes in PHP 7.1 by ars on Monday May 23, 2016 @12:12PM (#11754771)
> has been shortened to [] and now lets you specify keys (disclaimer: I was involved in both of these

Maybe you can answer this for me.

Will this work:

  $f = [$c, $d] = [1, 2];
i.e. set $c and $d to 1 and 2, then return the array and store in $f.
Re:Upcoming changes in PHP 7.1 by meritt on Monday May 23, 2016 @12:24PM (#11754846)
It should because:

    $f = list($c, $d) = [1, 2];
does exactly as you describe and this is literally just a shorthand syntax for accessing list().
Re:Upcoming changes in PHP 7.1 by ars on Monday May 23, 2016 @12:36PM (#11754927)
Thanks. What about:

  $c = 2;
  $d = 3;

  $f = [$c, $d] = [1, 2];
Is $f: 2, 3 or 1, 2? I assume 1, 2 because if not then it's both an array constructor and destructurer at the same time which would be interesting.

Plus:

  $f = $c = $d;
is 3;
Re:Upcoming changes in PHP 7.1 by claar on Monday May 23, 2016 @01:34PM (#11755394)
Let's break this up a bit, remembering that assignment is right-associative:

  $c = 2; # Assign 2 to $c
  $d = 3; # Assign 3 to $d

  [$c, $d] = [1, 2]; # Assign 1 to $c, assign 2 to $d

  $f = [$c, $d]; # Assign [1, 2] to $f
Re:Upcoming changes in PHP 7.1 by jonhohle on Monday May 23, 2016 @02:49PM (#11755930)
From a grammar perspective, it seems odd to declare property types as prefixes instead of matching the return type syntax. Were suffixed types considered?

  class Person
  {
      public $name : string;
      public $age : int;
      public $isEmployee : bool;
  }
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @04:04PM (#11756482)
Well, we don't use suffixes for parameter types:

  public function __construct(string $name, int $age, bool $isEmployee);
And all other qualifiers (`static`, `public`/`protected`/`private`) come before the property name, so why should the type be any different?

Hack doesn't do this either.

I do think the idea came up.

Re:Upcoming changes in PHP 7.1 by amochohan on Monday May 23, 2016 @09:22AM (#11753641)
I touched on the shortened [] syntax and non-numeric string arithmetic in the article.

Thank you for getting these added to the language, I think it's a fantastic improvement.

Re:Upcoming changes in PHP 7.1 by elgenie on Monday May 23, 2016 @07:27AM (#11753135)
It's pretty amusing to read some of the PHP RFCs and see the verbal gymnastics taken to avoid saying "look, we're just taking this directly from Hack" :)
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @07:32AM (#11753148)
There's certainly a lot of stuff inspired by Hack. If Hack did something well, sometimes PHP copies it. The syntax of all PHP 7's new type stuff is very close to Hack's.

Though the details may differ significantly. Hack and PHP's type systems are only superficially the same (AOT rather than runtime validation, disabling or ignoring type checks versus using "weak" type checks, type inference vs. no type inference, etc.), for example.

Re:Upcoming changes in PHP 7.1 by coldtea on Monday May 23, 2016 @07:58AM (#11753265)
Well, Hack was in turn based on PHP, so?
Re:Upcoming changes in PHP 7.1 by nkozyra on Monday May 23, 2016 @08:02AM (#11753283)
Some of this could also be attributed to the fact that Hack also borrowed heavily. While I'm sure a lot of it came from playing with Hack and saying "wow, this should be how PHP7 does things," it might also be recognition from use in other languages (Java specifically).
Re:Upcoming changes in PHP 7.1 by agumonkey on Monday May 23, 2016 @08:18AM (#11753342)
python, php, es6 ... Lisp did all before. (waiting for the smalltalkers to pitch in).

In a less facetious spirit, it's interesting to see languages converge a lot. Darwinian gene dynamics.

Re:Upcoming changes in PHP 7.1 by untog on Monday May 23, 2016 @03:33PM (#11756263)
What's wrong with that? Hack was based on PHP, no? It's like CoffeeScript and JavaScript - lots of the incredibly useful features in CoffeeScript found their way into ES6.