35

PHP Annotated – February 2020 | PhpStorm Blog

 4 years ago
source link: https://blog.jetbrains.com/phpstorm/2020/02/php-annotated-february-2020/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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);
    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', '[email protected]');
    $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
    class Person
    {
        protected $name;
        protected $email;
        public function __toArray()
        {
            return [
                'name' => $this->name,
                'email'  => $this->email,
            ];
        }
    }
    
    $person = new Person('John Doe', '[email protected]');
    
    $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) {}
    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

96b65e92179a40f2bd1884549973ddd5.png Symfony

314bd0f0dfc54e3fa7f0c0daef1a2d25.png Laravel

pjlkob5btqut7it5e_eod-qtqh0.png 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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK