https://www.php.net/releases/8.0/en.php

  • Union Types (for properties that might need to hold values of different types)

  • Named arguments (so that your functions can have more parameters without bogging your users down in piles of useless default arguments). Guess what? Now you have to be more responsible in how you name your function parameters because they're now a visible part of your API.

  • Match expressions (they're like a more compact switch statement, or a more sophisticated (and type-safe) lookup table, that can appear inside an expression; for when an array isn't enough but a switch is too much).

  • Attributes (instead of relying on ad-hoc doc comment annotations that might or might not make sense to your IDE).

  • Constructor Property Promotion (when all your constructor does is initialise a bunch of properties from its arguments, now all three steps can be done at once).

  • Nullsafe access operator ($foo->bar() is an error if $foo happens to be null. $foo?->bar() is null if $foo is null).

  • Weak maps (a weak map won't stop its contents from being garbage-collected; if a value isn't used anywhere else, the weak map will discard it as well).

  • Etc. etc.

A new major version of PHP has just been released, with quite a few new language features, some old sillinesses corrected, and, oh, a JIT compiler.

Because major versions are where backward-compatibility breaks are allowed, reading the migration documentation is a must. For example, features that were deprecated in PHP 7 are now gone. Error reporting is more aggressive. A number of resource types have been replaced by classes.

But possibly the most important thing to pay attention to is the fact that 0=="foo" is now false instead of the old and infamous silliness of true. What happens now when you compare a number to a string is that the string is checked to see if it even looks like a number. Strings that don't look like numbers aren't equal to numbers.

    Weedpacket changed the title to PHP 8 Released🎉.

      sneakyimp Is it just me or was PHP 7 very short-lived?

      Eh...it's been around for years, but it seems like a lot of the web stayed on 5 for man7 of those years (including a non-trivial amount still today).

      Compared to PHP 6, 7 has been around infinitely times longer. 😉

      PS: Seems like at least a couple of those new features may have been Ruby-inspired, e.g. the null-safe operator, along with the named function/method arguments. (Then again, the Ruby syntax may well have been inspired by something else?)

      sneakyimp PHP 7.0 was launched five years ago, but v5.0 was 12 years before that. On the other hand v4.0 came out four years before 5.0.

      So it's more like PHP 5 was very long-lived. Almost like it was covering for 6. (Really, 5.3/5.4 were a rebranded 6.)

        NogDog I think C# copied the null-safe access from Ruby as well. (Semantically, Haskell was there much earlier, with its Maybe monad, and there's probably a library out there that wraps it up in a ?-> operator. But then, Haskell's Either monad was being used for exception handling). Named arguments are one of the things I've long envied Python for.

        I'm a little worried about attributes. The way Python implements them at least makes it awfully easy to put functionality in there so that the code you're looking at doesn't do what it says it does and there can be all sorts of spooky action-at-a-distance going on (one little attribute on an innocent little property and suddenly there's 50kB of ORM generated and your innocent little $b = $foo->bar; is opening a remote database connection — who'd've thought a line like that could fail because of a network problem?).

        11 days later

        Just noticed this line in the incompatibility guide:

        • Disabled functions are now treated exactly like non-existent functions. Calling a disabled function will report it as unknown, and redefining a disabled function is now possible.

        There's a potential gotcha: with a function disabled it disappears and can be redeclared as something completely different. So if you've ever been annoyed with a built-in function's behaviour then now's your chance to rewrite it to suit yourself. [PS: Don't do this.]

          3 years later
          Write a Reply...