PHP Annotated — August 2021

Greetings everyone!
PHP 8.1 has reached the second beta and the list of changes has been finalized. The final piece left to decide on is the nullability of intersection types, which is being voted on right now.

Meanwhile, PHP internals are already discussing features for the next release. Read on to learn about the first two RFCs for PHP 8.2, which is coming in 2022.

As PHP 8.1 is going to have fibers, two popular async frameworks for PHP have agreed to join forces and work together on shared abstraction.

You can read more about this news in the August edition of PHP Annotated. As usual, we’ve carefully selected a collection of articles, tools, videos, and streams for you.

News

PHP 8.1.0, Beta 2

The beta releases signal the end of the active development phase of PHP 8.1, which means that the list of new features and changes can be considered final (with the exception of nullable intersection types). The third and final beta is expected August 19.

Image by Peter Kokot.

So what’s coming in PHP 8.1?
Here is a shortlist of the most notable changes:

Enums (RFC).
First-class callables (RFC).
Readonly properties (RFC).
New operator in initializers (RFC).
Brand new never type for return values (RFC).
Fibers (RFC).
Final constants in classes (RFC).
Spread operator supporting string array keys (RFC).
Deprecation of implicit non-integer-compatible float to int conversions (RFC).
Deprecation of serializable interfaces (RFC).
Explicit octal notation with the 0o prefix (RFC).
Restricted usage of $GLOBALS (RFC).

If you’d like to learn more, check out this awesome compilation of PHP 8.1 “before” and “after” examples.

There is also a 10-day newsletter from Brent Roose: The Road to PHP 8.1.

And a full description of all the changes is available on PHP.Watch: php.watch/versions/8.1.

PHP.Watch also has a post on Directory Uploads in PHP 8.1 – In PHP 8.1 the $_FILES array will contain a full_path section with directory structure paths from the user. This is needed to support directory uploading.

ReactPHP and AmpPHP join forces

With the addition of fibers in PHP 8.1, asynchronous PHP code may be a little easier and prettier. The maintainers of two popular libraries for asynchronous PHP have announced the RevoltPHP project. They plan to make a common event loop and implement shared abstraction over fibers.

With Fiber in 8.1 a new age dawns for #PHP. @AsyncPHP and @ReactPHP are joining forces through @RevoltPHP collaborating on a shared event loop and Fiber abstraction. Stay tuned! pic.twitter.com/pv8q0WmmvG

— Revolt PHP (@revoltphp) August 9, 2021

PhpStorm 2021.2 has been released

Check out this blog post that contains all the details about generics support, enums, array shapes, and everything else that is being introduced in this release.

PHP 7.4.22, PHP 8.0.9

Security and bugfix updates for current branches.

PHP Internals

[RFC] Nullable Intersection types

Shortly after the adoption of the RFC on type intersections, a comment was posted by Nikolas Grekas (Symfony core). He said that without the ability to make them nullable, the intersections are much less interesting.

Technically, the PHP 8.1 branch is already frozen, but the PHP team decided to make an exception for this proposal.

class Foo
{
public (X&Y)|null $bar;

function setBar((X&Y)|null $bar = null): (X&Y)|null
{
return $this->bar = $bar;
}
}

Syntax choice is also put to a vote: ?X&Y or (X&Y)|null.

[RFC] Unwrap reference after foreach

In the current versions of PHP, there is no dedicated scoping for the foreach loop. This has a very counterintuitive consequence: the reference to the $value and the last element of the array remain even after the foreach loop ends.

The problem is easily illustrated by this example:

$array = [0, 1, 2];
foreach ($array as &$value) { /* … */ }
// without calling unset($value),
// $value still points to the last element: $array[2]

foreach ($array as $value) {
// $array[2] will be updated each time with the values of $array
}
var_dump($array);

// Before RFC:
>
array(3) {
[0] => int(0)
[1] => int(1)
[2] => &int(1)
}

// After RFC:
> array(3) {
[0] => int(0)
[1] => int(1)
[2] => int(2)
}

This RFC proposes a fix for this logically correct, but buggy-looking, behavior. After the foreach loop, the reference to $value will be removed.

[RFC] Never For Parameter Types

In PHP 8.1 there will be a new type, never, for return values. For PHP 8.2, there is already a proposal to use never as the type for parameters.

The author of the RFC, Jordan LeDoux, thinks that never will be useful in abstract classes and interfaces. You could use it to specify that the parameter must be typed in some way.

This could help to mimic the behavior of generics. However, you will still have to resort to the type descriptions in PHPDoc for static analyzers and PhpStorm.

interface CollectionInterface {
public function add(never $input): self;
}

class A implements CollectionInterface {
public function add(int $input): self {}
}

Tools

frontdevops/php-evil – An extension to disable eval(). It turns out that eval() is not a function, but a language construct, so disable_functions will not help to forbid it. So the author had to write his own micro-extension.
beyondcode/expose 2.0 – A cool tool for sneaking local services into the world. This should be useful for testing webhooks or demoing apps.
EcotoneFramework – An interesting service bus for DDD, CQRS, and Event Sourcing in PHP. It works in PHP 8+ via attributes, with good documentation and integrations for Laravel and Symfony. Here is an example of a CQRS implementation.
PHP-CSS-Parser – A Parser for CSS files written in PHP. This allows the extraction of CSS files into a data structure, manipulation of said structure, and output as (optimized) CSS.
Psalm 4.9.0 – The first community release of Psalm after Matt Brown, the author, left the project.
ergebnis/composer-normalize – A plugin for Composer that makes the composer.json file look nice.
paragonie/paseto 2.0 – Stateless-token, a safe alternative to JWT.
chrome-php/chrome v1.0.0 – A package to control Chrome/Chromium in headless mode.
darsyn/ip – Immutable value objects for working with IP addresses, including helpers for Doctrine
MadLisp – A Lisp interpreter written in PHP.

Symfony

The Symfony team now provides support in the Discussions section on GitHub.
Scaling the Symfony Demo app to the extreme with Varnish.
Symfony Lock: dealing with shared resources, concurrency and parallelism.
A way to centrally handle Doctrine/Symfony access control.
How to use cascade delete in Doctrine and not lose all the data.
A week of Symfony #763 (9-15 August 2021).

Laravel

Laracon Online – Scheduled for September 1.
laravelexamples.com – A catalog of useful examples for Laravel from the author of the Laravel Daily YouTube channel.
LaravelDaily/Laravel-Roadmap-Learning-Path – A cool collection of Laravel training materials for all experience levels.
Building an app to scale on Laravel Vapor.
Immutable dates in Laravel.
60+ Laravel collection methods in 15 minutes.
What’s new in Laravel v8.53.0 – Check out more videos on Laravel YouTube channel.

Yii

Yii framework team continues to release packages for the upcoming Yii 3: yiisoft/data-response, yiisoft/view, yiisoft/yii-view, yiisoft/mailer.

PhpStorm

i18n Ally – A nice plugin for PhpStorm for making easy work of translation strings. It’s automatically configured for Laravel and Symfony, and it can be manually configured for Yii, CodeIgniter, and CakePHP.
Laravel Idea 4.4 – A big update to the plugin.
Source diving the PhpStorm Pest plugin – Cool stream recording where Oliver Nybroe shows how to develop PhpStorm plugins.

Misc

We don’t need runtime type checks – Interesting musings on the type system in PHP by Brent Roose, with quotes from Sarah Golemon, Rasmus Lerdorf, and Nikita Popov. Here’s another opinion on the subject from Joe Watkins:

Removing types from #PHP, even at runtime:

1) fundamentally changes the dynamic nature of PHP
2) will complicate the type system considerably
3) is not a solution to the complexity of a generics implementation

Source: I’ve experimented.

TL;DR it isn’t happening …

— Joe Watkins (@krakjoe) August 3, 2021

The evolution of a PHP object throughout the years
Functional programming and PHP 8 – With examples of how to use the powerful loophp/fpt tool.
phpschool.io: PHP 8 Appreciate – A CLI workshop for learning PHP 8.
b-viguier/php-emoji – PHP array functions explained with emojis:

Videos

What’s new in PHP 8.1 – Enums – A detailed tour of enums, by Marcel Pociot.
Event Sourcery Full Course – CQRS and Event Sourcing.
PHP Roundtable 82 – The return of the legendary PHP podcast. Cheers Sammy K and welcome new host Eric Van Johnson!

Thanks for reading, we hope you have a great day!

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.3.4 released!

PHP 8.2.17 released!

PHP 8.1.27 released!

Generated by Feedzy