PHP Annotated – March 2021

Greetings everyone,

In this edition of PHP Annotated, read about the latest proposals for PHP 8.1, releases of xdebug.cloud, RoadRunner 2.0, and the newly proposed PSR ClockInterface standard. And as usual, we’ve got a load of articles, tools, videos, and streams carefully selected for you.

News

PHP 8.0.3, PHP 7.4.16 – Regular bug-fix updates of the supported branches.

xdebug.cloud
A new paid service from the author of Xdebug, which aims to simplify the debugging of PHP applications in more complex network configurations, such as when using Docker, Vagrant, or cloud services.
WordPress share exceeds 40% among the top 10 million sites
Learn more about the methodology used by w3techs and the historical trends.

New PSR ClockInterface standard proposed for consideration
The interface is needed so that there is a single way to get the current time, which can be easily mocked in tests.

The following version is currently in a draft:

namespace PsrClock;

interface ClockInterface
{
/**
* Returns the current time as a DateTimeImmutable Object
*/
public function now(): DateTimeImmutable;
}

PSR-11 Container Interface updates published
As with other standards, two versions were released at once: 1.1, which added parameter types, and 2.0 – with a return type.

Composer 1.x is no longer supported
Starting in May 2021, access to the old APIs on packagist.org will be restricted.

PHP Internals

[RFC] Enumerations
PHP 8.1 will have long-awaited enums!
enum RfcStatus {
case Draft;
case UnderDiscussion;
case Accepted;
}

function setRfcStatus(RfcStatus $status) :void {
// …
}

setRFCStatus(RfcStatus::Accepted); // Ок
setRFCStatus(‚Draft‘); // TypeError
If you like the enums feature, please consider sponsoring the authors of this RFC: Ilija Tovilo and Larry Garfield.

Read more about enums in Brent Roose’s article and some more on php.watch.

Symfony has already opened tickets to add enums support.

[RFC] Deprecate passing null to non-nullable arguments of internal functions
In the current versions of PHP, standard functions can accept null as an argument when the parameter is not nullable without any errors.

To fix this inconsistency, in PHP 8.1 built-in functions will throw a Deprecation notice, and in PHP 9 they will throw a TypeError. For example, str_contains(„“, null). 3v4l.org/OVoa0A.

[RFC] Array unpacking with string keys
Unpacking arrays will work consistently in PHP 8.1, including arrays with string keys: $array1 = [‚a‘ => ‚apple‘, ‚p‘ => ‚pear‘];
$array2 = [‚b‘ => ‚banana‘, ‚o‘ => ‚orange‘];
$array = […$array1, …$array2];
// The same as:
$array = array_merge($array1, $array2);

[RFC] PHPiterableany() and all() on iterables
The proposal to add any() and all() functions for iterators did not pass the vote.

[RFC] var_representation() : readable alternative to var_export()
The idea to add an alternative for var_export also did not find support, so for now we can use a userland alternative brick/varexporter.

? [RFC] Fibers
Voting on Fibers has started. In short: this is a small but important improvement to generators that endeavors to make writing asynchronous PHP code easier. For example, like this:

Creating two concurrently executed #fibers with Amp v3 is as simple as this. #PHP pic.twitter.com/FSgXOnUy6u

— Amp (@asyncphp) March 12, 2021

The Scheduler was removed from the RFC, so the Fiber API now provides the bare minimum and is similar to the resembling feature in Ruby.

There were some reasonable arguments against the proposal from Swoole maintainers. As well as from Joe Watkins, the author of krakjoe/parallel extension.

Yet, fibers seem to be a real step in the direction of asynchronous capabilities, which does not contradict either Swoole or parallel.

? [RFC] mysqli bind in execute
Kamil Tekiela continues the initiative to improve mysqli. This RFC proposes adding a new optional parameter to mysqli_stmt::execute() that will take an array of values and bind them automatically. Currently, you have to make a separate call to mysqli_stmt::bind_param(), which only accepts variables passed by reference.

[RFC] New in initializers
In the current versions of PHP, initializers such as the default values of properties, parameters, and constants can only have constant values.

When non-constant values are needed, properties can be initialized in a constructor, and arguments can be initialized in the method’s body. With constants, there are no such options at all.

Nikita Popov proposes to make it possible to use objects as default values for properties, parameters, and any constants and static variables.

static $x = new Foo();

const C = new Foo();

class Bar
{
public const CONSTANT = new Foo();
public static Foo $staticProperty = new Foo();

#[AssertAll([new AssertUuid()])]
public array $uuids = [];

public function __construct(
private Logger $logger = new NullLogger()
) {}
}

function test($param = new Foo()) {}

For now, the proposal is limited to the new operator, but the implementation allows extending support for other expressions in the future.

An additional bonus (or an intention? ) is that objects will be allowed in the attributes. That solves the problem with nested attributes.

[RFC] CachedIterable (rewindable, allows any key&repeating keys)
Tyson Andre suggests adding a caching iterator. It stores the state of an iterator and internally contains immutable copies of its keys and values.

[RFC] Namespaces in bundled PHP extensions
All classes and functions provided by bundled PHP extensions are currently located in the global namespace (with one exception).

This RFC proposes a convention on using namespaces with bundled extensions. Basically, the extension name should be used as a namespace and no PHP prefix is needed. For example, OpenSSLCertificate can become OpenSSLCertificate.

So far, however, this only applies to new symbols added, and the migration of existing functions and classes is not affected by this RFC. But the examples show possible future transformations:
str_contains() -> Stringcontains()
in_array() -> Arraycontains().

[RFC] noreturn type
The authors of Psalm and PHPStan suggest adding a new type to PHP – noreturn.

The authors of Psalm and PHPStan suggest adding a new type to PHP – noreturn. This would be a bottom type indicating that a function either always throws an exception or terminates execution, i.e. calls exit(), die(), trigger_error().

function redirect(string $uri): noreturn {
header(‚Location: ‚ . $uri);
exit();
}

function redirectToLoginPage(): noreturn {
redirect(‚/login‘);
}

There is a similar type in Hack and in Python, and it has been used in Psalm, PHPStan, and also in PhpStorm as an attribute #[NoReturn] or via an exitPoint() in .phpstormmeta.php.

PhpStorm

Mastering PhpStorm – A video course by Christoph Rumpel is live now!
PhpStorm 2021.1 EAP – The Early Access Program is drawing to a close. This release focuses on stability and performance rather than adding new features, but you can try out new inspections and quick-fixes, an HTML and PHP preview in the editor, JSONPath support, pair programming with audio and video calls, and a lot more.
Setup Step Debugging in PHP with Xdebug 3, Docker Compose, and PhpStorm.

? Tools

spiral/RoadRunner v2.0.0 – A major release of the PHP application server written in Golang.
azjezz/psl – A standard library for PHP, inspired by hhvm/hsl.
renoki-co/php-k8s – The package allows managing Kubernetes resources from within PHP.
Comparison of markdown libraries for PHP: erusev/parsedown, thephpleague/commonmark, cebe/markdown, michelf/php-markdown.
phpize.online – A tool for sharing PHP code, similar to 3v4l.org but with SQL support.

Symfony

symfony/runtime – A new experimental component decouples the application from the global state and allows running it unchanged on PHP-FPM, CLI, PHP-PM, Swoole, etc.
symfony/panther 1.0 – A Symfony component based on the Selenium WebDriver for E2E testing and scraping.
In In Symfony 6, the minimum version of PHP will be 8.0.
Symfony packages are not tagged anymore when nothing changes between versions.
How to use the Symfony Lock component effectively, using Messenger as an example.

Laravel

Laravel Octane – At Laracon Online, Taylor Otwell presented a universal package to run high performant Laravel on top of either Swoole or RoadRunner.
Testing HTTP middleware in Laravel.
A thread with tips on Laravel performance.
RCE vulnerability in Laravel debug mode – An investigation of an attack vector using file_get_contents / file_put_contents and passive FTP mode. The issue was in the facade/ignition component.
Laravel 7 support has ended – But the sixth version of the framework is LTS and is supported until September 2022.
spatie/laravel-remote – Runs Artisan commands on the remote server using spatie/ssh. Learn more in this stream with Freek Van der Herten, in which he develops the package from scratch.
Step-by-Step Guide to Building Your First Laravel Application.

Yii

Yii news 2021, issue 1
Fresh releases of Yii 3 components: yiisoft/files, yiisoft/var-dumper, yiisoft/auth-jw, yiisoft/log, yiisoft/event-dispatcher, yiisoft/network-utilities, yiisoft/csrf.

Misc

Complete guide to FFI in PHP.
Character escape sequences and numeric notations in PHP.
Unit testing tips by examples in PHP.
When to use empty in PHP? Benjamin Eberlei says never.
PHP 8: Observability Baked Right In – Article about internal API for tracing functions.
When Objects Are Not Enough – Interesting thoughts on OOP.
PHP benchmarks on arm64 vs x86_64 – AWS with ARM is 20% cheaper and in some cases faster.
Moving From Nginx+FPM to Swoole Has Increased Our PHP API Performance by 91% – The story of github.com/appwrite.
How Laminas CI automation works – GitHub Actions to support, test, and release 200+ packages.

? Videos

PHP Release Radar #5 – With Frank de Jonge about Flysystem 2.
PHP Release Radar #6 – With Matthieu Napoli about Bref 1.0.
PHP Release Radar #7 – With Kevin Dunglas and Antoine Bluchet about API Platform 2.6 and Panther 1.0.

? Podcasts

php[architect] podcast #50 – Discussed the Mezzio framework, functional programming, and software dependency security.
PHP Ugly podcast:
#227: – Math is Hard.
#226: – Help Wanted, PHP Release Manager.
PHP Internals News podcast:
#75 – With Nikita Popov on deprecating null, array unpacking, restrict globals usage, and phase out serializable.
#76 – Another one with Nikita Popov about restricting the use of globals, and phasing out serializable.
#77 – With David Gebler about the fsync() function added to PHP 8.1.
#78 – With Andreas Heigl on moving PHP documentation to Git.

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

The PhpStorm Blog : The Lightning-Smart IDE for PHP Programming | JetBrains Blog
Read More

Latest News

PHP-Releases

PHP 8.0.23 released!

PHP 8.1.10 released!

PHP 7.4.30 released!