PHP Annotated – September 2020

PHP Annotated Monthly

Greetings everyone,

I hope you are all ready for September’s installment of PHP Annotated!

In it, we have the additional PHP 8 beta 4 release, the PHP community’s momentum with the generics discussion in Internals, and the Community Synergy Initiative. There are some interesting releases like Laravel 8, Composer 2.0 RC 1, and PoC for PHP support for GraalVM. And as usual, we’ve got a load of useful articles, tools, videos, and podcasts for you too.

⚡️ News & Releases

🐘 PHP Internals

  • [RFC] Shorter Attribute Syntax Change — The epic story of the syntax for attributes is finally over. The #[Attribute] option has won the vote.
    #[
      ORM\Entity,
      ORM\Table("user")
    ]
    class User
    {
        #[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue]
        private $id;
    
        #[ORM\Column("string", ORM\Column::UNIQUE)]
        #[Assert\Email(["message" => "The email '{{ value }}' is not a valid email."])]
        private $email;
    }
    

    By the way, the previous syntax @@Attr did not support attribute grouping and therefore this feature was removed from PR. But since #[ ] has an end marker, support for grouping has returned.

    // You can do this way
    #[ORM\Entity]
    #[ORM\Table("user")]
    
    // or this way
    #[
        ORM\Entity,
        ORM\Table("user")
    ]

    Learn more about attributes in this overview post.

  • 🆕 [RFC] any() and all() on iterables — In this RFC Tyson Andre proposes to add two new functions to the standard lib:
    any(iterable $input, ?callable $callback = null): bool — executes the callback on each element and stops at the first that returns true.
    all(...) — will only return true if the callback returns true for each element.

    Here is an example of it in action:

    // Before
    $satisifes_predicate = false;
    foreach ($item_list as $item) {
        if (API::satisfiesCondition($item)) {
            $satisfies_predicate = true;
            break;
        }
    }
    if (!$satisfies_predicate) {
        throw new APIException("No matches found");
    }
    
    // After
    if (!any($item_list, fn($item) => API::satisfiesCondition($item))) {
        throw new APIException("No matches found");
    }
  • 🆕 Runtime-erased generics proposal — Brent Roose, whom you might know from his posts on Laravel and PHP, came up with the idea of adding generics to PHP, but without any checks in the runtime.

    Let’s say we have the following code with generics:

    class Collection<T> {
    
        public function add(T $item) { ... }
    
    }
    $c = new Collection<Product>().
    

    Static analyzers and IDEs will be able to parse it and perform analysis. However, the PHP interpreter is supposed to ignore generics and execute the code as follows:

    class Collection {
    
        public function add(mixed $item) { ... }
    
    }
    $c = new Collection().
    

    This is similar to how generics work in Hack language by default. And in Python, for example, almost all the information about types is removed in most cases and no popular interpreter validates types of arguments.

  • 🆕 Observer API — PHP 8 will have an internal API to track when functions are entered or left. This is useful for extensions such as Xdebug, profilers, and APM solutions like New Relic, Tideways, and so on.

    Find out more about the API in the 🔈 PHP Internals News podcast #68 with the authors Levi Morrison and Sammy K Powers.

🛠 Tools

Symfony

Laravel

Yii

🌀 Async PHP

💡 Misc

📺 Videos

🔈 Podcasts

And last but not least, check out the new PHP 8 logo!
PHP 8
And a joker ConFoo elePHPant with PHP 8 logo is available for preorder (minimum 6 pc.).


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