PHP Annotated – October 2020

Roman Pronskiy

PHP Annotated Monthly

Greetings everyone!

In our October edition of PHP Annotated you can read about the upcoming PHP 8, Xdebug 3 beta, short functions, and multiline arrow functions proposals in Internals. And as usual, we’ve got a load of useful articles, tools, videos, and podcasts to tell you about, too.

⚡️ News & Releases

  • PHP 8.0.0 RС 2 – The release candidate series has started for PHP 8. There are at least two more release candidates expected before the final release.

    A separate branch was created for 8.0, and the master branch now targets PHP 8.1.

    Overviews of new features in PHP 8 can be found in the following blog posts: What’s new in PHP 8 and PHP 8: before and after. There is also a PhpStorm blog post and video devoted to using them in the IDE, as well as a look at the new features by Larry Garfield and an overview of what has changed on PHP.Watch.

    You can try PHP 8 with Docker using a php:rc-cli image, on Mac using shivammathur/homebrew-php, or you can compile from source with php-build.

    And you can subscribe for updates on the Front Line PHP, the upcoming ebook on building modern applications with PHP 8 by Brent Roose & Freek Van der Herten.
  • PhpStorm 2020.3 EAP – The early access program for PhpStorm lets you try out the new features that are coming in the next release. PHP 8 support, PHPStan, and Psalm already work out of the box, and there are many other things. The second EAP build even brings support for the first trivial case for generics via @template.
  • PHP 7.2.34
  • PHP 7.3.23
  • PHP 7.4.11
  • Xdebug 3.0 beta 1 – In this third version, the configuration was redesigned to make it easier to start working with the debugger. Just a single option is required: xdebug.mode=debug. The default port has been changed from 9000 to 9003 – so there are no longer any conflicts with php-fpm, which also uses port 9000. Learn more in the upgrade guide.
  • Composer 2.0.0 RC2 – This is the last release candidate before the final version. To try it now, run: composer self-update --preview.

🐘 PHP Internals

  • [PR] Attributes on property groups – Attributes can now be specified for an entire property group, instead of just one by one. This works exactly the same as it does for access modifiers.
    class FooBar {
        #[NonNegative]
        public int $x, $y, $z;
    }
    
  • [PR] Attributes and strict types – Attributes will also take into account the strict_types=1 directive.
  • [PR] OPCache: Direct execution opcode file without php source code file– With this PR, the author proposes making it possible to save an opcache binary file and run it without the source code – similar to how the Python .pyc / .pyo files do it.

    However, the discussion about it revealed some problems with this approach. The opcode format in PHP is unstable, and different versions are incompatible with each other. Even between patch releases, for instance, code compiled in PHP 7.4.22 can simply fail from segfault on PHP 7.4.23. Making it stable is very unlikely to happen.
  • In a recent AMA with the PhpStorm team on Reddit, Nikita gave a detailed answer on the status and prospects of generics in PHP. In short, runtime-erased generics is the most viable approach from a purely technical point of view, but at the same time, it is very inconsistent and leaves a hole in type safety.
  • [PR] Multiline arrow functions– Arrow functions added in PHP 7.4 can only contain a single expression. This PR implements multiline short lambdas:
    $guests = array_filter($users, fn ($user) => {
      $guest = $repository->findByUserId($user->id);
      return $guest !== null && in_array($guest->id, $guestsIds);
    });
    

    One of the advantages this has over usual lambda-functions is scope capturing, as there is no need to add use.

    There is also a question regarding syntax, namely whether or not to add an arrow (=>): fn() => {} vs. fn() {}
  • 🆕 [RFC] Short Functions– This RFC proposes to bring arrow function syntax for named functions and methods that are simple return expressions.
    function pick_one(int $a) => match($a) {
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        default => 'More',
    };
    
    print pick_one(1) . PHP_EOL;
    

    class Person
    {
        public function __construct(
            private string $firstName,
        ) {}
    
        public function getFirstName(): string => $this->firstName;
    }
    

🛠 Tools

  • thephpleague/event 3.0.0 – his popular event package is now compatible with PSR-14.
  • terrylinooo/simple-cache – PSR-16 simple cache drivers for files, Redis, MySQL, SQLite, APC, APCu, Memcache, Memcached, and WinCache.
  • Bolt 4.0 – Symfony 5 CMS baked into a composer package, with out-of-the-box REST, GraphQL API, and i18n.
  • PHP-DI – A practical and framework-agnostic dependency injection container.
  • markrogoyski/math-php – A powerful modern math library for PHP.
  • Danack/FloatHex – Functions for converting float to a hex string and back again, and also for showing two floating point numbers as a binary representation. Or, to put it this way one more time, shows why 0.1 + 0.2 === 0.3 -> false
    echo float_compare(0.3, 0.1 + 0.2);
    >>
    ┌──────┬─────────────┬──────────────────────────────────────────────────────┐
    │ Sign │ Exponent    │ Mantissa                                             │
    │    0 │ 01111111101 │ 0011001100110011001100110011001100110011001100110011 │
    │    0 │ 01111111101 │ 0011001100110011001100110011001100110011001100110100 │
    │    - │ ----------- │ -------------------------------------------------xxx │
    └──────┴─────────────┴──────────────────────────────────────────────────────┘
    
  • marcocesarato/PHP-Antimalware-Scanner – A tool to scan PHP files and analyze your project to find any malicious code inside it.
  • Prometheus PHP – Unofficial client libraries for prometheus.io in PHP.
  • shivammathur/setup-php – GitHub action for setting up PHP, extensions, and other things to prepare the environment in your pipelines. There is a short overview on the GitHub blog.

Symfony

Laravel

Yii

  • W3C will move on from WordPress and will instead use CraftCMS, which is based on Yii 2. The news itself would not be so interesting if it were not for this thorough document which explains how the decision was made.

🌀 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

Subscribe

Subscribe for updates