PHP Annotated – October 2020
Greetings everyone!
In our October edition of PHP Annotated you can read about the upcoming PHP 8, Xdebug 3 beta, short functions, and multiline arrow functions proposals in Internals. And as usual, we’ve got a load of useful articles, tools, videos, and podcasts to tell you about, too.
⚡️ News & Releases
- PHP 8.0.0 RС 2 – The release candidate series has started for PHP 8. There are at least two more release candidates expected before the final release.
A separate branch was created for 8.0, and themaster
branch now targets PHP 8.1.
Overviews of new features in PHP 8 can be found in the following blog posts: What’s new in PHP 8 and PHP 8: before and after. There is also a PhpStorm blog post and video devoted to using them in the IDE, as well as a look at the new features by Larry Garfield and an overview of what has changed on PHP.Watch.
You can try PHP 8 with Docker using a php:rc-cli image, on Mac using shivammathur/homebrew-php, or you can compile from source with php-build.
And you can subscribe for updates on the Front Line PHP, the upcoming ebook on building modern applications with PHP 8 by Brent Roose & Freek Van der Herten. - PhpStorm 2020.3 EAP – The early access program for PhpStorm lets you try out the new features that are coming in the next release. PHP 8 support, PHPStan, and Psalm already work out of the box, and there are many other things. The second EAP build even brings support for the first trivial case for generics via
@template
. - PHP 7.2.34
- PHP 7.3.23
- PHP 7.4.11
- Xdebug 3.0 beta 1 – In this third version, the configuration was redesigned to make it easier to start working with the debugger. Just a single option is required:
xdebug.mode=debug
. The default port has been changed from9000
to9003
– so there are no longer any conflicts with php-fpm, which also uses port9000
. Learn more in the upgrade guide. - Composer 2.0.0 RC2 – This is the last release candidate before the final version. To try it now, run:
composer self-update --preview
.
🐘 PHP Internals
- ✅ [PR] Attributes on property groups – Attributes can now be specified for an entire property group, instead of just one by one. This works exactly the same as it does for access modifiers.
class FooBar { #[NonNegative] public int $x, $y, $z; }
- ✅ [PR] Attributes and strict types – Attributes will also take into account the
strict_types=1
directive. - [PR] OPCache: Direct execution opcode file without php source code file– With this PR, the author proposes making it possible to save an opcache binary file and run it without the source code – similar to how the Python
.pyc / .pyo
files do it.
However, the discussion about it revealed some problems with this approach. The opcode format in PHP is unstable, and different versions are incompatible with each other. Even between patch releases, for instance, code compiled in PHP 7.4.22 can simply fail from segfault on PHP 7.4.23. Making it stable is very unlikely to happen. - In a recent AMA with the PhpStorm team on Reddit, Nikita gave a detailed answer on the status and prospects of generics in PHP. In short, runtime-erased generics is the most viable approach from a purely technical point of view, but at the same time, it is very inconsistent and leaves a hole in type safety.
- [PR] Multiline arrow functions– Arrow functions added in PHP 7.4 can only contain a single expression. This PR implements multiline short lambdas:
$guests = array_filter($users, fn ($user) => { $guest = $repository->findByUserId($user->id); return $guest !== null && in_array($guest->id, $guestsIds); });
One of the advantages this has over usual lambda-functions is scope capturing, as there is no need to add
use
.
There is also a question regarding syntax, namely whether or not to add an arrow (=>
):fn() => {}
vs.fn() {}
- 🆕 [RFC] Short Functions– This RFC proposes to bring arrow function syntax for named functions and methods that are simple return expressions.
function pick_one(int $a) => match($a) { 1 => 'One', 2 => 'Two', 3 => 'Three', default => 'More', }; print pick_one(1) . PHP_EOL;
class Person { public function __construct( private string $firstName, ) {} public function getFirstName(): string => $this->firstName; }
🛠 Tools
- thephpleague/event 3.0.0 – his popular event package is now compatible with PSR-14.
- terrylinooo/simple-cache – PSR-16 simple cache drivers for files, Redis, MySQL, SQLite, APC, APCu, Memcache, Memcached, and WinCache.
- Bolt 4.0 – Symfony 5 CMS baked into a composer package, with out-of-the-box REST, GraphQL API, and i18n.
- PHP-DI – A practical and framework-agnostic dependency injection container.
- markrogoyski/math-php – A powerful modern math library for PHP.
- Danack/FloatHex – Functions for converting float to a hex string and back again, and also for showing two floating point numbers as a binary representation. Or, to put it this way one more time, shows why
0.1 + 0.2 === 0.3 -> false
echo float_compare(0.3, 0.1 + 0.2); >> ┌──────┬─────────────┬──────────────────────────────────────────────────────┐ │ Sign │ Exponent │ Mantissa │ │ 0 │ 01111111101 │ 0011001100110011001100110011001100110011001100110011 │ │ 0 │ 01111111101 │ 0011001100110011001100110011001100110011001100110100 │ │ - │ ----------- │ -------------------------------------------------xxx │ └──────┴─────────────┴──────────────────────────────────────────────────────┘
- marcocesarato/PHP-Antimalware-Scanner – A tool to scan PHP files and analyze your project to find any malicious code inside it.
- Prometheus PHP – Unofficial client libraries for prometheus.io in PHP.
- shivammathur/setup-php – GitHub action for setting up PHP, extensions, and other things to prepare the environment in your pipelines. There is a short overview on the GitHub blog.
Symfony
- Dynamically changing the log level in Symfony apps.
- A Week of Symfony #720 (12-18 October 2020)
- New Symfony core team members.
- What’s coming in Symfony 5.2 – The upcoming Symfony release will be compatible with PHP 8 among other things, and Symfony 5.2 brings some PHP 8 attributes. For example:
#[Route('/path', name: 'action')]
for routing,#[Required]
to specify dependencies, and also attributes for controller parameters like#[CurrentUser]
to get the user:public function index(#[CurrentUser] MyUser $user) { /* ... */ }
Laravel
- ksassnowski/venture – This package allows building complex workflows of interdependent jobs using Laravel’s queuing system.
- spatie/laravel-typescript-transformer – Create TypeScript types based on PHP classes for use on the frontend. Learn more about the motivation behind it in the post.
- Spatie’s updated PHP and Laravel guidelines.
- spatie/spatie.be – The folks at Spatie have open-sourced the code of their spatie.be website. And they have also released а series of tutorials with all the details of the implementation.
- The complete guide to Laravel frontend scaffolding.
- 📺 Laravel Worldwide Meetup #3: Exploring The Magic Of PhpStorm – Adel Faizrakhmanov, I’d Like To Speak To Your Manager – Yaz Jallad.
- 📺 Stream: Generate dynamic open graph images for freek.dev blog posts.
- 📺 Taylor answers questions regarding Jetstream.
Yii
- W3C will move on from WordPress and will instead use CraftCMS, which is based on Yii 2. The news itself would not be so interesting if it were not for this thorough document which explains how the decision was made.
🌀 Async PHP
- micc83/mailamie – A simple SMTP catch-all server for testing. Built on top of ReactPHP.
- clue/reactphp-mq – A lightweight in-memory message queue.
💡 Misc
- Transpiling PHP code from 8.0 to 7.x via Rector – If it is not possible to upgrade to a newer version of PHP, but you want to use the new features in development, you can add a “compile” stage to the project and downgrade with the help of rector/rectorphp.
- Error suppression operator
@
usage analysis. - PHP 8.0 named parameters and variadic – Discusses the different ways of calling functions with an arbitrary number of arguments and using the spread operator (
...
). - .gitattributes for PHP projects.
- Instructions on how to write tests for PHP itself – You don’t need to know C for this, because the tests are written in PHP.
- Set up PhpStorm to use PHP with PHPUnit and Xdebug in Docker.
- Caching makes everything faster. Right? – A post from the PHPUnit author about why adding caching for code coverage in PHPUnit did not have the desired effect and how this problem was finally solved.
- Analysis of a PHP script that distributes Emotet malware.
- Which colour scheme is better? – According to science, light themes are better than dark ones.
- Joe Watkins: Being Free, and how to support PHP.
📺 Videos
- Profiling PHP applications with Xdebug – Stream with Derick Rethans, author of Xdebug.
- Tutorial on starting with rectorphp/rector.
- Compiling and test driving PHP 8 – Brent Roose and Freek Van der Herten set up PHP 8 and look at some of its new features.
- Deploy Friday #27 – Language Spotlight: PHP – A podcast featuring PHP 8 release managers Sara Golemon and Gabriel Caruso, hosted by Larry Garfield and Patrick Dawkins.
🔈 Podcasts
- PHPUgly 210: About new docker set ups, migrating from Laravel 5.7 to 8, queuevms.
- PHPUgly 209: About more drama in the Laravel world, DigitalOcean’s new app platform, reviewing pull request.
- Voices of the ElePHPant interviews:
• With Oliver Davies
• With Miriam Schwab and Zeev Suraski
• With Jaap van Otterdijk
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.
Your JetBrains PhpStorm team
The Drive to Develop
Prev post Video: What’s Coming in PhpStorm 2020.3 – Episode 1PhpStorm 2020.3 EAP #4: Custom PHP 8 Attributes Next post