Upcoming changes in PHP 7.1
dotdev.co
dotdev.co
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).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.
In a less facetious spirit, it's interesting to see languages converge a lot. Darwinian gene dynamics.
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.
> 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.
PHP's built-in functions documented as "void" also return NULL, so I suppose there's an argument from consistency.
There's a lot of code that uses "return;" to mean "return null;" and honestly I don't see it as a big problem.
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...
I'll take this opportunity to say thank you, for this, and all the other hard work you put into PHP internals.
Thank you for getting these added to the language, I think it's a fantastic improvement.
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. $f = list($c, $d) = [1, 2];
does exactly as you describe and this is literally just a shorthand syntax for accessing list(). $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; $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 $fI 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.
* 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.
class Person
{
public $name : string;
public $age : int;
public $isEmployee : bool;
} 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.
> Support class constant visibility
Reminds me of what Rob Pike once said, "[Java, JavaScript (ECMAScript), Typescript, C#, C++, Hack (PHP), and more] are converging into a single huge language".
Bob says "I wish PHP had this thing from Java" and Alice says "I wish Java had this thing from PHP". I don't think it's necessarily a bad thing.
EDIT "pthreads v3 is restricted to operating in CLI only" - from the project page.
Is there any good introduction for people how "know" PHP4 and want to get started with PHP7, using best practises?
It looks like PHP implemented SERVER_PUSH [0] via libcurl [1], openly described as a "work in progress."
I'm sure all the code is "well written", both in PHP and libcurl, but I can't shake the feeling that we'll see at least one significant vulnerability as a result of this new feature. The two github issues on the feature, one referencing a segfault caused by a double-free [2], and one caused by server-specific protocol issues [3], do not help to assuage my worry.
[0] https://wiki.php.net/rfc/curl_http2_push
[1] https://daniel.haxx.se/blog/2015/06/03/server-push-to-curl/
As for frameworks, I and others have already been going around (for years before the mcrypt deprecation came up) switching things to use OpenSSL (and, for that matter, authenticated encryption) instead of mcrypt.
https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcr...
To date: ZF, CodeIgniter, Symfony, Laravel, and CakePHP all support OpenSSL instead of Mcrypt, as do many, many more.
You get E_DEPRECATED in 7.1 then in 7.2 you can still
pecl install mcrypt
...if you really have a need to support abandonware."Learning Laravel" is a known plagiarist, who has a habit of ripping off content from other Laravel sites.
The "ads by carbon" certainly don't cast this particular website in a good light, either.
Facebook and other sites make it clear that what you're looking at is a summary meant to lead you to another site - they don't disguise it as content.
Don't ignore all the things.
He has even stolen content from other Laravel books and published it as his own book.
EDIT: Dang to the rescue.
... PHP 7.1 introduces visibility modifiers to constants ...
... With PHP 7.1 it is now possible to specify that a function has a void return type, i.e. it performs an action but does not return anything ...
... Example four contains numeric values, so everything else is stripped out, and the sum of those are used to calculate the total of 10. But it would still be nice to see a warning, as this may not be intended behaviour ...
Good to see PHP continues to introduce new inconsistencies while striving to break existing code by fixing old ones. Best of both worlds!
edit: OK this was uncharitable and technically some of them are not inconsistencies, just things I thought were weird. I know PHP is widely used and considered useful and will stay with us forever. But I found those snippets interesting.
Moreover, having these declarations actually be part of the language, even if the interpreter itself cannot enforce them ahead-of-time, means tooling can be built around it, and we can use them for optimisations in the VM (which PHP 7.1 does: we elide type checks on certain arithmetic operations for operands of known types).
If you want to ensure type safety before the program actually runs, you can of course let your IDE or static analyser check that for you.
I think types specifications in interpreted (not compiled) languages should be only hints (for IDEs, code analyzers and JIT compilation etc.). This type checking for every method probably also adds some overhead does it not?
EDIT: correction
Unenforced type hints don't give these guarantees. They have no requirement to be accurate because the runtime won't enforce them, so they're essentially documentation, not code. They can't be safely relied upon by the optimiser because they're unenforced. The code inside the function body can break at runtime if given the wrong type, so if having a specific type is particularly important, you have to add manual type checks or coercions yourself.
Furthermore, doing ahead-of-time checking would require infrastructure we don't currently have for providing information to the PHP interpreter about whether or not PHP files have passed type checks. But again, most code is not fully typed, and so this doesn't provide all that many guarantees to the interpreter.
Having methods that just have the right names is a pretty weak assurance, isn't it?
Visibility and typing features are good. Visibility helps to design and write better code. The typing features are also welcomed in my view. I would rather a request completely fails because a variable is the wrong type rather than PHP's type coercion leading to an unexpected result with no errors or warnings.
That said, I use PHP for my day job. I see no reason to start new projects in it. I never use it in my side projects. Introduction of the single pipe operator to represent OR instead of a double pipe operator is baffling in my opinion.
7.1 does more work to improve consistency and error handling which is definitely welcomed. The problem is there is much legacy crap in PHP. It only exists for web applications and there are a growing number of languages which do this better.
I kind of understand it since it's not a traditional OR operation and intends to keep and associate the matching values, but maybe something like
catch (Exception $e in (Exceptions...))
would make it clearer. Either way, it's so close to feeling like an OR operation that I feel like it will be a lot of debugging when you forget this.
Because they are easy to implement (function type hints, typed properties) and the php community have a collective, insatiable, perpetual hardon for features that are supposed to enhance correctness..
So the core devs are eager to propose these kinds of features. And the community is eager to accepts them without much thought..
To see how child like this mentality towards these type of features, see this comment, where a Php expert makes a big deal regarding when a user omitted mentioning the type of a value....
https://www.reddit.com/r/PHP/comments/4k2htl/ive_just_given_...
The good it does is it stops your application from possibly continuing on with buggy code.
This is especially important in something like billing code. Imagine if your code was expecting an integer (bill amount) but got a string instead that via the types system evaluated to 0 or 1 instead of the real integer... In that case you might charge customers less or more than you should have and its a big deal trying to get all that fixed.
Better to just throw an error which you can see in an exception handling system like Sentry and take care of the problem.
... "catch (FooException | BarException" is new syntax that would be a include-time fatal in existing code
... "public const FOO" is new syntax that would be a include-time fatal in existing code; public is the default modifier in the language when no visibility modifier is present, so existing code functions exactly the same as before
... ": void" return type only conflicts with existing code if a class is named Void, which becomes an error for the file declaring the class ... this is vanishingly unlikely [0] and an easy grep to fix; if you're looking at some code "$var = i_return_void();", the annotation is a clear sign that the "$var =" part is obviously confused.
... " 5 + 'a string' " is nonsense code which codebases that care about warnings currently want to be aware of.
[0] even the few hits for https://github.com/search?utf8=%E2%9C%93&q=%22class+Void+%22... are mostly false positives.
|| is a short-cutting or comparison operator which returns the first true thing it encounters.
| is a bitwise or combinator, which takes two sets of flags, and combines them into one set of flags that has all flags activated which were also active in the input sets.
Semantically, | is absolutely the correct choice here.
A better question would be why there's a $2 there, though i hope that's just a typo and means $e.
Choosing a pipe because it's easier for an obscure language feature, is not least astonishment. Commas are used for lists. The pipe is a bad choice among a nice feature.
I understand the use of pipes for bitwise, but in this case if you read the code in the example, we're basically saying "if the exception is of type A or of type B", personally I'd be inclined to use || if I didn't already know the syntax.
I wish. The fact that it actually doesn't is one of the things that really bugs me about PHP:
<?php
var_dump(0 || 42);
The above example will output: bool(true) $ php -r 'var_dump(0 || 42);'
bool(true)
$ php -r 'var_dump(0 ?: 42);'
int(42)edit: Just to clarify for anyone else unfamiliar with it:
$ php -r 'var_dump(42 ?: 0);'
int(42) $ php -a
Interactive shell
php > echo 1 || die('die');
1
This echo's 1, it doesn't die.