PHP Annotated – August 2020

PHP Annotated Monthly

Greetings everyone,

Please welcome the August installment of PHP Annotated!

PHP 8 has reached feature freeze and received a few last big additions: the nullsafe operator, named arguments, and saner string to number comparison. The syntax for attributes, though, has not yet been decided. Read the full story behind the attributes from the initial proposal to the latest revote and the battle between #[] and @[]. And as usual, we’ve got a load of useful articles, tools, videos, and podcasts for you in this issue.

⚡️ News & Releases

🐘 PHP Internals

  • 🍿 [RFC] Shorter Attribute Syntax Change — The syntax for attributes in PHP 8 is still not decided yet.

    The syntax for attributes in PHP 8 is still undecided. First, Benjamin Eberlei proposed << >> and it was accepted. Later there was voting for a new syntax with 3 options: << >>, #[], and @@. The latter won, probably as it is the one most similar to PHPDoc tags and Java annotations. However, it turned out @@ has some parsing issues, as well as a bunch of other problems.

    The #[Attr] syntax has some considerations too: 1, 2.

    And now it’s been decided to have another revote to choose from one of the following syntax options: @@Attr, #[Attr], <<Attr>> и @[Attr]. Reactions from the community have been in good humor:

    Anyway, there’s no need to worry because we’ll get used to any syntax… eventually.

    It’s hard to believe that back in 2008, instead of \ for the namespace separator people considered **, ^^, :>, :), :::, and %%.

    Well, there is always a plan B to satisfy everyone and no-one at the same time.

  • [RFC] Saner string to number comparisons — The proposal that breaks backward compatibility passed almost unanimously.

    In PHP 8, when comparing numbers and strings with non-strict ==, both operands will be converted to a string and compared as strings if any of them is not a numeric string.

    0 == 'foobar' is now officially false.

    Saner string to number comparisons

    It also entails a change in the behavior of all comparison operators: <=>, ==, !=, >, >=, < and <=, the switch construct, and functions such as in_array(), sort() and others.

  • [RFC] Treat namespaced names as a single token — In PHP 8, the entire namespace is considered a single token. This allows keywords to be used within the namespace, for example, namespace app\function { class Foo {} } and avoids potential backward compatibility issues when introducing new keywords.

    With this change, namespaces can no longer contain comments, which means anything this ugly should never see the light of day again:

    use /** Try comments */ \FullyQualified \ /* in this ugly way */ SometTotallyDifferentTrait /** also after */;
  • [RFC] Nullsafe operator — The new operator ?-> has been accepted for PHP 8.
    Instead of lots of null checks, you will be able to write elegant one-liners such as

    $country = $this->sessionStorage->getSession()?->getUser()?->address?->country;
  • [RFC] Named Arguments — PHP 8 will have named arguments/parameters! It will now be possible to pass values to a function or method based on the parameter name, not just its position.
    htmlspecialchars($string, ENT_COMPAT | ENT_HTML401 , ini_get("default_charset"), false);

    will become:

    htmlspecialchars($string, double_encode: false);

    Learn more about named arguments in the post stitcher.io/blog/php-8-named-arguments.

  • [RFC] Named Parameters explicit opt in — The document has been redesigned and now, instead of renaming the parameters, it proposes only to explicitly indicate that the parameter is a named one.

    Since PHP 8 has already reached the feature-freeze stage, this proposal may only come about in 8.1.
  • [Proposal] Bigint shorthand (123n) for GMP objects — A very interesting proposal for the next versions, with no official RFC so far.

    Tyson Andre proposes to implement a syntax for large numbers by adding an "n" suffix, similar to how it’s done in JavaScript:

    $theBiggestInt = 9007199254740991n

    Under the hood, this will create GMP objects and will allow us to use them just like normal numbers in PHP, because the arithmetic, bitwise, and comparison operators are already overloaded for GMP objects.

🛠 Tools

Symfony

Laravel

💡 Misc

📺 Videos

🔈 Podcasts

And last but not least, you can now get original Blue or Pink elephpants from their designer Vincent Pontier on www.elephpant.com. Gray and Black ones are also available at shop.symfony.com with worldwide shipping.


Thanks for reading!

If you have any interesting or useful links to share via PHP Annotated, please leave a comment on this post or send me a tweet.

Subscribe to PHP Annotated

Your JetBrains PhpStorm team
The Drive to Develop

image description