10 ms·
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,
by TazeTSchnitzel 10y ago
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).
- elgenie 10y agoIt'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" :)
- TazeTSchnitzel 10y agoThere'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.
- deleted 10y ago[deleted]
- coldtea 10y agoWell, Hack was in turn based on PHP, so?
- nkozyra 10y agoSome 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).
- agumonkey 10y agopython, 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.
- untog 10y agoWhat'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.
- arenaninja 10y agoWhat 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 http://php.net/manual/en/functions.returning-values.php
- blowski 10y agoSelf-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.
- arenaninja 10y agoI 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.
- blowski 10y agoIt'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_nullpermitted 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.
- deleted 10y ago[deleted]
- tokenizerrr 10y agoThis is the same as other languages. In for example Java you have `public void method()`, and you then `return` which results in a `null`
- blowski 10y ago> 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.
- amochohan 10y agoI 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.
- ars 10y ago> 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.
- meritt 10y agoIt should because: $f = list($c, $d) = [1, 2]; does exactly as you describe and this is literally just a shorthand syntax for accessing list().
- ars 10y agoThanks. 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;
- claar 10y agoLet'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
- djsumdog 10y agoI'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.
- raiph 10y agoWhat 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.
- jonhohle 10y agoFrom 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; }
- TazeTSchnitzel 10y agoWell, 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.