maettig.com

Thiemos Archiv

After I wrote similar blog posts about PHP 5.4, 5.5 and 5.6, here is (finally) my list of new PHP 7.0 features I'm looking forward to use.

TL;DR, in order of personal preference:

  • Type-hint methods with return types: function getUser(): User
  • Start using bool, int, float, and string type hints.
  • Use "\u{2603}" in strings.
  • Use $value = $array['optional'] ?? 'default'; instead of isset() or array_key_exists().
  • Make anonymous classes with new class.
  • Enjoy the freedom to use foreach more wildly.
  • Still avoid unserialize(), but when you use it, list the allowed classes.

What changed in PHP 7.0, in more detail:

A ton of fixes have been made between PHP 5.6 and 7.0 that are – in a sense – breaking changes. More precisely: Older PHP versions allowed to write code that would behave very weird in edge-case situations, e.g. when using $$ magic, pointer magic, or magic string unpacking. I would argue this was all broken. All you need to keep in mind is this: PHP 7 behaves much more sane.

There is not much I find worth mentioning from the breaking changes section because of this:

  • The /e modifier in regular expressions got removed. It allowed to have code in a string, similar to eval(). Such code can now very easily be rewritten to use preg_replace_callback() instead.
  • A lot more exceptions are thrown now, instead of these weird »internal runtime errors« we had to catch with custom error handlers before.
  • foreach does not mess with the internal array pointer (the one you interact with when using reset(), end(), etc.) any more.
  • foreach also operates on a copy of the array now. The loop is now allowed to manipulate the array without messing with the loop. Before, adding elements to the array in the loop could have made it an endless loop.
  • All ereg functions are finally removed. Hurray! They have been a pain because of their abysmal runtime.
  • Support for the <% … %> tags – a weird, »ASP-compatible« alternative to the <?php … ?> tags – got removed.
  • Hexadecimal strings are not considered to be numeric any more.

What's new in PHP 7.0?

  • Return types: function getUser(): User
  • Strict type hints for the scalar types bool, int, float, and string.
  • Unicode code points in strings: "\u{2603}"
  • The ?? null coalescing operator I blogged about before.
  • The <=> spaceship operator behaves similar to what you might already know from strcmp(). Both compare two values and return -1, 0, or 1 depending on the result of the comparison.
  • Anonymous classes via new class {}.
  • unserialize() allows to list class names that are allowed when unserializing serialized objects.
  • intdiv() was added. It suprises me quite a bit to see this happening in – ugh – 2015. If there was one detail I remember from my time with QBasic in the 1990s it is the benefit of having an integer division operator, in BASIC done via \, in contrast to the regular floating-point division via /.
  • dirname() got an optional $levels parameter. This allows to simplify dirname( dirname( … ) ) chains.
  • Array constants: define( 'CHARACTERS', [ 'a', 'b' ] ). Note the same was already possible in PHP 5.6 when using the const keyword in a class.
  • use statements allow grouping via an ugly curly bracket syntax. No, just no. Please don't use this.
  • preg_replace_callback_array() allows to run a bunch of replacements at once. The changelog briefly mentions something like this would have been possible via preg_replace_callback() before. Ugh, I think I don't want to know.

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

[ ← Zurück zur Übersicht ]

Impressum & Datenschutz