PHP Annotated – April 2020

Posted on by Roman Pronskiy

php_annotated

Greetings everyone,

This April edition of PHP Annotated overviews 10 new RFCs from PHP Internals, Composer 2.0 alpha, and other releases, as well as articles on Laravel and Symfony, best practices, useful tools, videos, and podcasts.

⚡️ News & Releases

  • PHP 7.4.5, PHP 7.3.17, and PHP 7.2.30 are released.
  • Composer v2.0 Alpha – April 5 marked exactly 9 years since the first commit to the Composer repository. You can now see what’s new and what has changed in the upcoming second major release. To try it right away, run: composer self-update --snapshot.
  • PhpStorm 2020.1 is out.

🐘 PHP Internals

  • The PHP 8 release schedule is available. The first alpha is expected on June 18, and the final release is planned for December 3.
  • [RFC] Constructor Property Promotion – Larry Garfield has published a detailed analysis of PHP’s object ergonomics. Larry concludes that it would be best to focus on these three RFCs: constructor promotion, named parameters, and compound Property Visibility.
    And in the wake of the analysis, this RFC proposes allowing you to declare properties directly in the constructor. Instead of writing this:
    class Point {
        public float $x;
        public float $y;
        public float $z;
    
        public function __construct(
            float $x = 0.0,
            float $y = 0.0,
            float $z = 0.0
        ) {
            $this->x = $x;
            $this->y = $y;
            $this->z = $z;
        }
    }
    
    It will be possible to use the following:
    class Point {
        public function __construct(
            public float $x = 0.0,
            public float $y = 0.0,
            public float $z = 0.0
        ) {}
    }
    
  • [RFC] Allow trailing comma in parameter list – There is also a proposal to allow a comma to be used after the last parameter in a function definition. This has already worked since PHP 7.3 for the list of arguments in a function call.
    class Uri {
        private function __construct(
            ?string $scheme,
            ?string $user,
            ...
            ?string $query,
            ?string $fragment // <-- ARGH!
        ) {
            ...
        }
    }
    
  • [RFC] Stricter type checks for arithmetic/bitwise operators – This RFC offers to generate a TypeError when an arithmetic or bitwise operator is applied to an array, resource, or object.
    var_dump([] % [42]);
    // int(0)
    // WTF?
    
  • [RFC] Type casting in array destructuring expressions – See an example:
    [(int) $now, (int) $future] = ["2020", "2021"];
    
    // The same as the following
    [$now, $future] = ["2020", "2021"];
    $now = (int) $now;
    $future = (int) $future;
    
  • [RFC] throw expression – This proposal has been accepted, and in PHP 8 throw will no longer be a statement. Rather it will be an expression. This means it will be possible to throw an exception in arrow functions, ternary operators, and other constructions.
    $callable = fn() => throw new Exception();
    
    $foo = $bar['key'] ?? throw new KeyNotSetOnBarException();
    
  • [RFC] Attributes v2 – The proposal to bring attributes (annotations) into PHP core by Benjamin Eberlei and Martin Schröder has been greatly improved.
    It looks like the proposal has a good chance to pass. The syntax is also being voted on: <<>> vs. @:.
    <<ExampleAttribute>>
    class Foo
    {
        <<ExampleAttribute>>
        public $x;
    
        <<ExampleAttribute>>
        public function foo(<<ExampleAttribute>> $bar) { }
    }
    
  • [RFC] Mixed Type v2 – At the moment, if a function does not have its return type specified, it is not clear whether the developer forgot to specify it or deliberately chose not to specify it for some reason. Besides, the mixed pseudotype is already used in the PHP manual.
    Máté Kocsis and Danack suggest adding it in the PHP 8. The mixed type will be equivalent to a union of array|bool|callable|int|float|null|object|resource|string types.
  • [RFC] non-capturing catches – This is a proposal to make a variable declaration optional in the catch block:
    try {
        changeImportantData();
    } catch (PermissionException) { // The intention is clear: exception details are irrelevant
        echo "You don't have permission to do this";
    }
    
  • [RFC] Match expression – Instead of fixing the switch construct Ilija Tovilo proposes to introduce a match expression, that is devoid of all the switch flaws, such as the absence of type checks and the possibility to return a value. It can also be extended to pattern-matching in the future.
    $expressionResult = match ($condition) {
        1, 2 => foo(),
        3, 4 => bar(),
        default => baz(),
    };
    
  • [RFC] Pipe Operator v2 – This is the second attempt to introduce the |> operator for sequential function calls while passing the result of the previous function as an argument to the next one.
    $result = "Hello World"
        |> 'htmlentities'
        |> 'explode'
        |> fn($x) => array_map(fn($v) => 'strtoupper', $x)
        |> fn($x) => array_filter(fn($v) => $v != 'O');
    
  • Rejected: Server-Side Request and Response Objects, Userspace operator overloading, Write-Once (readonly) Properties, Compact Object Property Assignment.

🛠 Tools

  • jlaswell/compote – A lightweight dependency management for PHP written in Go. It is not designed as a replacement for Composer but rather as a compliment for specific use-cases, CI for example. Currently, it can install locked dependencies and show locked dependencies for a project.
  • repman.io – A private PHP package repository manager for Composer.
    It also provides a proxy with CDN for packagist.org, which can speed up the download of packages.
  • dantleech/what-changed – A Composer plugin that generates change reports when you run composer update.
  • clue/graph-composer – This tool creates a graph of your project’s composer dependencies.
  • VKCOM/noverify v0.2.0 – A fast static analyzer written in Go for PHP. The release improves PHP 7 support, offers more diagnostics, and returns fewer false positives.
  • markrogoyski/math-php 1.0 – The first stable release of a math library for PHP.
  • ackintosh/ganesha 1.0 – A Circuit Breaker pattern implementation for PHP applications. Learn more in the blog post.
  • Spiral Framework – A high-performance hybrid PHP+Go framework.

Symfony

Laravel

Yii

Zend/Laminas

🌀 Async PHP

  • hyperf/hyperf – A framework for building microservices or middlewares based on Swoole coroutines.
  • clue/reactphp-flux – This package allows you to limit the number of simultaneously executed competitive tasks in ReactPHP.

💡 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

Stay safe!
Your JetBrains PhpStorm team
The Drive to Develop

Subscribe

Subscribe for updates