PHP Annotated — August 2021

PHP Annotated Monthly

Greetings everyone!
PHP 8.1 has reached the second beta and the list of changes has been finalized. The final piece left to decide on is the nullability of intersection types, which is being voted on right now.

Meanwhile, PHP internals are already discussing features for the next release. Read on to learn about the first two RFCs for PHP 8.2, which is coming in 2022.

As PHP 8.1 is going to have fibers, two popular async frameworks for PHP have agreed to join forces and work together on shared abstraction.

You can read more about this news in the August edition of PHP Annotated. As usual, we’ve carefully selected a collection of articles, tools, videos, and streams for you.

⚡️ News

  • PHP 8.1.0, Beta 2

    The beta releases signal the end of the active development phase of PHP 8.1, which means that the list of new features and changes can be considered final (with the exception of nullable intersection types). The third and final beta is expected August 19.


    Image by Peter Kokot.

    So what’s coming in PHP 8.1?
    Here is a shortlist of the most notable changes:

    • Enums (RFC).
    • First-class callables (RFC).
    • Readonly properties (RFC).
    • New operator in initializers (RFC).
    • Brand new never type for return values (RFC).
    • Fibers (RFC).
    • Final constants in classes (RFC).
    • Spread operator supporting string array keys (RFC).
    • Deprecation of implicit non-integer-compatible float to int conversions (RFC).
    • Deprecation of serializable interfaces (RFC).
    • Explicit octal notation with the 0o prefix (RFC).
    • Restricted usage of $GLOBALS (RFC).

    If you’d like to learn more, check out this awesome compilation of PHP 8.1 “before” and “after” examples.

    There is also a 10-day newsletter from Brent Roose: The Road to PHP 8.1.

    And a full description of all the changes is available on PHP.Watch: php.watch/versions/8.1.

    PHP.Watch also has a post on Directory Uploads in PHP 8.1 – In PHP 8.1 the $_FILES array will contain a full_path section with directory structure paths from the user. This is needed to support directory uploading.

  • ReactPHP and Amp join forces

    With the addition of fibers in PHP 8.1, asynchronous PHP code may be a little easier and prettier. The maintainers of two popular libraries for asynchronous PHP have announced the RevoltPHP project. They plan to make a common event loop and implement shared abstraction over fibers.

  • PhpStorm 2021.2 has been released

    Check out this blog post that contains all the details about generics support, enums, array shapes, and everything else that is being introduced in this release.

  • PHP 7.4.22, PHP 8.0.9

    Security and bugfix updates for current branches.

🐘 PHP Internals

  • [RFC] Nullable Intersection types

    Shortly after the adoption of the RFC on type intersections, a comment was posted by Nikolas Grekas (Symfony core). He said that without the ability to make them nullable, the intersections are much less interesting.

    Technically, the PHP 8.1 branch is already frozen, but the PHP team decided to make an exception for this proposal.

    class Foo
    {
        public (X&Y)|null $bar;
    
        function setBar((X&Y)|null $bar = null): (X&Y)|null
        {
            return $this->bar = $bar;
        }
    }
    

    Syntax choice is also put to a vote: ?X&Y or (X&Y)|null.

  • [RFC] Unwrap reference after foreach

    In the current versions of PHP, there is no dedicated scoping for the foreach loop. This has a very counterintuitive consequence: the reference to the $value and the last element of the array remain even after the foreach loop ends.

    The problem is easily illustrated by this example:

    $array = [0, 1, 2];
    foreach ($array as &$value) { /* ... */ }
    // without calling unset($value),
    // $value still points to the last element: $array[2]
    
    foreach ($array as $value) {
    // $array[2] will be updated each time with the values of $array
    }
    var_dump($array);
    
    // Before RFC:
    >
    array(3) {
        [0] => int(0)
        [1] => int(1)
        [2] => &int(1)
    }
    
    // After RFC:
    > array(3) {
        [0] => int(0)
        [1] => int(1)
        [2] => int(2)
    }
    

    This RFC proposes a fix for this logically correct, but buggy-looking, behavior. After the foreach loop, the reference to $value will be removed.

  • [RFC] Never For Parameter Types

    In PHP 8.1 there will be a new type, never, for return values. For PHP 8.2, there is already a proposal to use never as the type for parameters.

    The author of the RFC, Jordan LeDoux, thinks that never will be useful in abstract classes and interfaces. You could use it to specify that the parameter must be typed in some way.

    This could help to mimic the behavior of generics. However, you will still have to resort to the type descriptions in PHPDoc for static analyzers and PhpStorm.

    interface CollectionInterface {
        public function add(never $input): self;
    }
    
    class A implements CollectionInterface {
        public function add(int $input): self {}
    }
    

🛠 Tools

  • frontdevops/php-evil – An extension to disable eval(). It turns out that eval() is not a function, but a language construct, so disable_functions will not help to forbid it. So the author had to write his own micro-extension.
  • beyondcode/expose 2.0 – A cool tool for sneaking local services into the world. This should be useful for testing webhooks or demoing apps.
  • EcotoneFramework – An interesting service bus for DDD, CQRS, and Event Sourcing in PHP. It works in PHP 8+ via attributes, with good documentation and integrations for Laravel and Symfony. Here is an example of a CQRS implementation.
  • PHP-CSS-Parser – A Parser for CSS files written in PHP. This allows the extraction of CSS files into a data structure, manipulation of said structure, and output as (optimized) CSS.
  • Psalm 4.9.0 – The first community release of Psalm after Matt Brown, the author, left the project.
  • ergebnis/composer-normalize – A plugin for Composer that makes the composer.json file look nice.
  • paragonie/paseto 2.0 – Stateless-token, a safe alternative to JWT.
  • chrome-php/chrome v1.0.0 – A package to control Chrome/Chromium in headless mode.
  • darsyn/ip – Immutable value objects for working with IP addresses, including helpers for Doctrine
  • MadLisp – A Lisp interpreter written in PHP.

Symfony

Laravel

Yii

PhpStorm

  • i18n Ally – A nice plugin for PhpStorm for making easy work of translation strings. It’s automatically configured for Laravel and Symfony, and it can be manually configured for Yii, CodeIgniter, and CakePHP.
  • Laravel Idea 4.4 – A big update to the plugin.
  • 📺 Source diving the PhpStorm Pest plugin – Cool stream recording where Oliver Nybroe shows how to develop PhpStorm plugins.

💡 Misc

📺 Videos


Thanks for reading, we hope you have a great day!

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