PHP Annotated – February 2020

Posted on by

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

, which will work similarly to , 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. [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.

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.

, , , etc., with a ServerRequest class, and ServerResponse instead of 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.

— 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

PHPUnit 9 — This release requires PHP 7.3+ and uses modern PHP features. It also has some backward-incompatible changes. Learn more in the migration instruction.

cycle/orm 1.2 — An ORM that can be used either as DataMapper or as ActiveRecord. In the latest release, the performance was improved by 33% and judging by these benchmarks, it is now one of the fastest ORMs.

lisachenko/z-engine — An experimental library that allows accessing the internal structures of PHP itself using FFI. There are examples of using it to implement immutable objects or modifying AST on a fly.

salsify/jsonstreamingparser — A JSON streaming parser for working with large files.

spatie/docker — This package provides a way to start docker containers and execute commands on them. Check the blog post to learn more.

symfony/polyfill-php80 — A PHP 8.0 polyfill.

BenMorel/weakmap-polyfill — A polyfill for WeakMap for PHP 7.4.

paratestphp/paratest — A tool that helps parallelize PHPUnit tests. Compatible with PHPUnit 9.

Symfony

Laravel

Zend/Laminas

🌀 Async PHP

📺 Interview with Marc Morera, an author of DriftPHP, about asynchronous PHP, DriftPHP and ReactPHP.

driftphp/awesome-reactphp — A curated list of resources for 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 tweet me.

Subscribe to PHP Annotated

Your JetBrains PhpStorm Team

The Drive to Develop