maettig.com

Thiemos Archiv

PHP 7.4 was finally released in November 2019. And this time we – the Wikimedia movement lead by the Wikimedia Foundation – are much faster catching up. It took just a few days to make it available on our continious integration cluster. There is a dedicated project to make sure all our code is able to run with PHP 7.4 as soon as possible.

As always, we will keep MediaWiki and all the software we write compatible with PHP 7.2 for a while, as long as this version is still supported. Which means we won't be able to use every cool new PHP feature right away. But sooner or later we will.

Here are the PHP 7.4 features I'm happily looking forward to use the moment I can:

TL;DR:

  • Use strict types in class properties.
  • Use ??= to assign default values.
  • Check all your fread and fwrite
  • Have fun with $mergedArray = [ ...$firstArray, ...$secondArray ];.
  • Replace trivial 1-line closures with fn ( ...$params ) => $returnValue.

And a more in-depth look:

  • Class properties can finally have types, e.g. public int $user;.
  • The new »null coalescing assignment operator« ??= allows to shorten $a = $a ?? $default; and similar if ( !isset( $a ) ) { $a = $default; } to $a ??= $default;. I will definitely use this quite a lot. Note the related »null coalescing operator« ?? already exists since PHP 7.0.
  • array_key_exists() got optimized and now runs even faster than isset().
  • Trying to access an array element from something that's not an array will still convert the thing to an array, but this will not happen silently any more. A notice will be triggered. I find this pretty significant. Finally, code that should always have failed will start talking to us.
  • fread() and fwrite() will now return false in case of an error. In previous PHP versions these functions would return 0 or an empty string. I kind of welcome this change, and fear it very much the same time. It means we absolutely must check and verify every single fread() and fwrite() call in all our code! It's absolutely possible an if ( !fread( … ) ) is not what we want our code to do now, even if this check would include both the old and the new return value. The same time it's possible an if ( fread( … ) === 0 ) will make our code start failing in case of a read error. There is no way to know other than carefully reviewing all this code a second time. Will we do this? Probably not. We will run into production errors and fix them after the fact, I'm afraid. I tried to look deeper into this change, and realized that nothing changed. The functions are documented to return false in case of a failure ever since. This section in the migration manual is wrong. I can only assume it refers to a fixed bug where some rare errors haven't been reported with false. But that's not a backwards incompatible change. Why is it listed as one?
  • We can use the ... operator inside of array definitions. This allows for a pretty awesome kind of array_merge(). It might even be possible to replace some array_splice() with this new feature.
  • The (real) cast and the is_real() function alias are deprecated. Use float.
  • mb_str_split() is new. Good to have, but in no way something I was looking for. The need for splitting strings into fixed-length chunks is, well, pretty rare.
  • Arrow functions allow to shorten function ( $a ) { return $a * $a; } to fn ( $a ) => $a * $a. To be honest I'm not sure how useful this is. It doesn't save much space. Instead of function it's fn, and the word return can be omitted. But the total amount of special characters one needs to type and read is the exact same. I don't expect to see this often.
  • It's said the two preg_replace_callback() functions would support an optional $flags parameter, accepting the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags (the later was introduced with PHP 7.2). But this is weird: The manual does not mention a $flags parameter, and the changelog misses it as well. So is this even true? Both got fixed.
  • There is a new interface for __serialize() and __unserialize(). The old serialize() and unserialize() will be deprecated.
  • The underscore character _ can be used to group long numbers. The same feature was introduced in Java 9 years ago.
  • strip_tags() accepts an array of tags. Definitely a welcome style change. But it's super rare to use this function with the second parameter.
  • Using $string{0} with {} brackets instead of [] is finally deprecated. Finally.
  • get_magic_quotes_gpc() still exists, but is deprecated now and always returns false.
  • imagecreatefromtga() is new. This continues a trend from PHP 7.2 which added support for the Windows BMP file format. I'm somewhat baffled. I mean, these formats are old. Really old. TGA exists since 1984, the GD library since 1994, PHP since 1995. What happened? What's the reason nobody was able to add these basic, pretty trivial file formats the past 25 years?

Kommentare zu diesem Beitrag können per E-Mail an den Autor gesandt werden.

[ ← Zurück zur Übersicht ]

Impressum & Datenschutz