PHP Annotated – June 2020

php_annotated

Greetings everyone,

Please welcome the June edition of PHP Annotated!
PHP internals have been very active and here are 3 accepted and 6 new RFCs, including alternative syntax for attributes — @@ and why #[] is better. There also has been some drama around renaming in the PHP world, and about PHP developers’ debugging practices. And as usual, a load of useful articles, tools, videos, podcasts, and more.

⚡️ News & Releases

🐘 PHP Internals

  • [RFC] Attribute Amendments — Attributes received a few additions: grouping capability <<Attr1, Attr2>>, renaming PhpAttribute to Attribute, validation, and the ability to add multiple identical attributes.
  • [RFC] Ensure correct signatures of magic methods — As for now, you can declare a magic method like this in a class:
    class Foo
    {
        function __get(int $name): void
        {
            echo $name;
        }
    }
    (new Foo)->{42};
    

    In PHP 8 however, signatures of magic methods will be validated and the code above will cause a compile-time error.

  • [RFC] Make sorting stable — All standard sort functions in PHP (sort, usort, asort, etc.) starting with PHP 8.0 will be stable. This means that the original element order with the same values is guaranteed. In current versions, it is easy to find examples where this is not the case.
  • [RFC] Opcache optimization without any caching — Declined.
  • [RFC] Make constructors and destructors return void — In current PHP versions, you can return any values from constructors and destructors, for example:
    class Test {
            public function __construct() {
                    return 0;
            }
    }
    
    $test = new Test();
    
    // this prints 0
    echo $test->__construct();
    

    Benas Seliuginas proposed to throw a Deprecated warning in such cases, and later in PHP 9.0 to remove this and generate a Fatal Error.

  • [RFC] Treat namespaced names as single token — Each element of the namespace is treated by the interpreter as a separate token. This is why there can be no keywords used in namespaces. For example, namespace app\function { class Foo {} }; will fail with a Parse Error. Nikita Popov proposed to consider the entire namespace as a single token as this would minimize backward compatibility problems when introducing new keywords.
  • [RFC] Rename T_PAAMAYIM_NEKUDOTAYIM to T_DOUBLE_COLON — The token :: in PHP is called T_PAAMAYIM_NEKUDOTAYIM — this fact was even pointed out as problem number one in the famous PHP Sadness list.
    George Peter Banyard and Kalle Sommer Nielsen suggested renaming it. We’re not sure this makes sense, as the error messages always note ::.
    Also, why remove a fun historical quirk?
  • [RFC] Shorter Attribute Syntax — Attributes have already been accepted for PHP 8, but many people do not like the syntax. Three options are being put to the vote: <<Attr>> (current) vs. @@Attr vs. #[Attr]. Brent Roоse had several convincing arguments for #[ ]:

    • It’s the same syntax as Rust, which is also a C-based language.
    • It’s compatible with old code: #[Attribute] will be just ignored by PHP <=7.4 as a comment.
    • @@ can be confused with the error suppress operator (example).
    • <<>> is also confusing, as it looks like bit-shift operators. In the future, it can be confused with generics that are likely to use the single angle brackets syntax <>.
    • In the meantime, @@ seems to be getting more votes.

  • [RFC] Change terminology to ExcludeList — The topic of renaming offensive terms has not been overlooked by the PHP world either. There have been heated discussions in the Internals.
    In the PHP core, the change affects only one thing: the configuration directive opcache.blacklist_filename should be renamed to opcache.exclude_list_filename.

    Many other PHP tools have already made changes in this regard: PHPUnit, Drupal, Xdebug, Yii, Composer (+ working with non-master Git branches).

    There is also a discussion on set of rules for PHP_CodeSniffer to find offensive words.

  • [RFC] Nullsafe operator — Instead of a bunch of nested conditions, Ilija Tovilo proposed to add the ability to refer to a property or method with a check for null:
    $country = $session?->user?->getAddress()?->country;

    Instead of

    $country =  null;
    
    if ($session !== null) {
        $user = $session->user;
    
        if ($user !== null) {
            $address = $user->getAddress();
    
            if ($address !== null) {
                $country = $address->country;
            }
        }
    }
    
  • PHP 8.0 release schedule was updated — The feature freeze has been moved to August 4, and the final release is scheduled for November 26.
  • You can try PHP 8 on 3v4l.org. Just check out the output in the php-master branch.

🛠 Tools

Symfony

Laravel

Yii

Zend/Laminas

🌀 Async PHP

💡 Misc

📺 Videos

🔈 Podcasts

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