PHP Annotated – February 2020

php_annotated

Greetings everyone,

It’s time for the February edition of PHP Annotated! This edition includes 4 new RFCs from PHP Internals and research results on Generics in PHP. We also cover PHPUnit 9 and other releases, articles on Laravel and Symfony, useful tools, videos, podcasts, and a whole lot more!

⚡️ News & Releases

🐘 PHP Internals

  • [RFC] Allow function calls in constant expressions — In the current PHP versions, you can only use literals, and operations in constant expressions. This RFC proposes to allow global functions calls in constant declarations, default values of static properties, static variables, and default values of function parameters.
    class MyClass {
        const VALUES = [1, 0];
        const C1 = count(self::VALUES);
    
        public static $p = count(self::VALUES);
    
        public function foo($param = count(self::VALUES))
        {
            static $bar = count(self::VALUES);
        }
    }
    
  • [RFC] __toArray() — This onу proposes to add a new magic method __toArray(), which will work similarly to __toString(), i.e. will be called on explicit type casting to array, or when an object is passed as an argument to a function with an array parameter declaration, or when it’s returned from a function with its return type specified as an array.
    class Person
    {
        protected $name;
        protected $email;
        public function __toArray()
        {
            return [
                'name' => $this->name,
                'email'  => $this->email,
            ];
        }
    }
    
    $person = new Person('John Doe', 'j.doe@example.com');
    
    $personArray = (array) $person; // Calls __toArray()
    
    function foo(array $person) {
        var_dump($person); // Array here
    }
    
    function bar(Person $person): array {
        return $person;
    }
    
    var_dump(bar($person)); // and here it's an array
    
  • [RFC] Userspace operator overloading — The proposal to add operator overloading is still in draft status, but is actively discussed in Internals. Meanwhile, you can try operator overloading in PHP 7.4 with FFI based on lisachenko/z-engine.
  • [RFC] Validation for abstract trait methods — Currently, signatures of implementation for abstract methods from traits are not validated. For example, this code now works without errors:
    trait T {
        abstract public function test(int $x);
    }
    
    class C {
        use T;
    
        // It works now, but it shouldn't because of a type mismatch
        public function test(string $x) {}
    }

    It’s proposed to correct this behavior.
    The patch was published as a pull-request, but it contains a backward incompatibility, that requires to pass the RFC-process: if then current code has an incorrect implementation of the method from a trait, the proposed change will cause an error.

  • [RFC] Server-Side Request and Response Objects v2 — The RFC proposes to replace superglobal arrays such as $_GET, $_POST, $_SERVER, etc., with a ServerRequest class, and ServerResponse instead of header() calls and sending content.
  • Generics in PHP — Recently, Nikita Popov has been working on a study of the possibility of adding generics to PHP. In short, according to Nikita, there are some serious difficulties, and he is not sure yet if adding complete generics to PHP is a good idea.
    There is a pull request with a prototype implementation and all the problems and open questions are detailed here: https://github.com/PHPGenerics/php-generics-rfc/issues/45.
  • php/doc-en — English documentation is now editable via pull-requests on GitHub instead of old edit.php.net.

🛠 Tools

Symfony

Laravel

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 tweet me.

Subscribe to PHP Annotated

Your JetBrains PhpStorm Team
The Drive to Develop

image description