Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today. Show
PHP 8 was released on November 26, 2020. You can download it here. It's a new major version, which means that there are some breaking changes, as well as lots of new features and performance improvements. Because of the breaking changes, there's a higher chance you'll need to make some changes in your code to get it running on PHP 8. If you've kept up to date with the latest releases though, the upgrade shouldn't be too hard, since most breaking changes were deprecated before in the 7.* versions. And don't worry, all these deprecations are listed in this post. Besides breaking changes, PHP 8 also brings a nice set of new features such as the JIT compiler, union types, attributes, and more. # New featuresLet's start with all new features, it's quite a list! # Union types RFCGiven the dynamically typed nature of PHP, there are lots of cases where union types can be useful. Union types are a collection of two or more types which indicate that either one of those can be used.
Note that
# JIT RFCThe JIT — just in time — compiler promises significant performance improvements, albeit not always within the context of web requests. I've done my own benchmarks on real-life web applications, and it seems like the JIT doesn't make that much of a difference, if any, on those kinds of PHP projects. If you want to know more about what the JIT can do for PHP, you can read another post I wrote about it here. # The nullsafe operator RFCIf you're familiar with the null coalescing operator you're already familiar with its shortcomings: it doesn't work on method calls. Instead you need intermediate checks, or
rely on
With the addition of the nullsafe operator, we can now have null coalescing-like behaviour on methods!
You can read all about the nullsafe operator here. # Named arguments RFCNamed arguments allow you to pass in values to a function, by specifying the value name, so that you don't have to take their order into consideration, and you can also skip optional parameters!
You can read about them in-depth in this post. # Attributes RFCAttributes, commonly known as annotations in other languages, offers a way to add meta data to classes, without having to parse docblocks. As for a quick look, here's an example of what attributes look like, from the RFC:
Note that this base Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter: # Match expression RFCYou could call it the big brother of the It looks like this:
You can read up on the match expression in detail, over here. # Constructor property promotion RFCThis RFC adds syntactic sugar to create value objects or data transfer objects. Instead of specifying class properties and a constructor for them, PHP can now combine them into one. Instead of doing this:
You can now do this:
There's a lot more to tell about property promotion, you can read about them in this dedicated post. # New static return type RFCWhile it
was already possible to return
# New mixed type RFCSome might call it a necessary evil: the
Because of the reasons above, it's a good thing the
Note that Also note that since
# Throw expression RFCThis RFC changes
# Inheritance with private methods RFCPreviously, PHP used to apply the same inheritance checks on public, protected and private methods. In other words: private methods should follow the same method signature rules as protected and public methods. This doesn't make sense, since private methods won't be accessible by child classes. This RFC changed that behaviour, so that these inheritance checks are not performed on
private methods anymore. Furthermore, the use of
# Weak maps RFCBuilt upon the weakrefs RFC that was added in PHP 7.4, a Take the example of ORMs, they often implement caches which hold references to entity classes to improve the performance of relations between entities. These entity objects can not be garbage collected, as long as this cache has a reference to them, even if the cache is the only thing referencing them. If this caching layer uses weak references and maps instead, PHP will garbage collect these objects when nothing else references them anymore. Especially in the case of ORMs, which can manage several hundreds, if not thousands of entities within a request; weak maps can offer a better, more resource friendly way of dealing with these objects. Here's what weak maps look like, an example from the RFC:
# Allowing ::class on objects RFCA small, yet useful, new feature: it's now possible to use
# Non-capturing catches RFCWhenever you wanted to catch an exception before PHP 8, you had to store it in a variable, regardless whether you used that variable or not. With non-capturing catches, you can omit the variable, so instead of this:
You can now do this:
Note that it's required to always specify the type, you're not allowed to have an empty # Trailing comma in parameter lists RFCAlready possible when calling a function, trailing comma support was still lacking in parameter lists. It's now allowed in PHP 8, meaning you can do the following:
As a sidenote: trailing commas are also supported in the # Create DateTime objects from interfaceYou can
already create a
# New Stringable interface RFCThe
# New str_contains() function RFCSome might say it's long overdue, but we finally don't have to rely on Instead of doing this:
You can now do this
# New str_starts_with() and str_ends_with() functions RFCTwo other ones long overdue, these two functions are now added in the core.
# New fdiv() function PRThe
new # New get_debug_type() function RFC
For example, calling A full list of differences between # New get_resource_id() function PRResources are special variables in PHP, referring to external resources. One example is a MySQL connection, another one a file handle. Each one of those resources gets assigned an ID, though previously the only way to know that id was to cast the resource to
PHP 8 adds the
# Abstract methods in traits improvements RFCTraits can specify abstract methods which must be implemented by the classes using them. There's a caveat though: before PHP 8 the signature of these method implementations weren't validated. The following was valid:
PHP 8 will perform proper method signature validation when using a trait and implementing its abstract methods. This means you'll need to write this instead:
# Object implementation of token_get_all() RFCThe # Variable syntax tweaks RFCFrom the RFC: "the Uniform Variable Syntax RFC resolved a number of inconsistencies in PHP's variable syntax. This RFC intends to address a small handful of cases that were overlooked." # Type annotations for internal functions EXTERNALSLots of people pitched in to add proper type annotations to all internal functions. This was a long standing issue, and finally solvable with all the changes made to PHP in previous versions. This means that internal functions and methods will have complete type information in reflection. # ext-json always available RFCPreviously it was possible to compile PHP without the JSON extension enabled, this is not possible anymore. Since JSON is so widely used, it's best developers can always rely on it being there, instead of having to ensure the extension exist first. # Breaking changesAs mentioned before: this is a major update and thus there will be breaking changes. The best thing to do is take a look at the full list of breaking changes over at the UPGRADING document. Many of these breaking changes have been deprecated in previous 7.* versions though, so if you've been staying up-to-date over the years, it shouldn't be all that hard to upgrade to PHP 8. # Consistent type errors RFCUser-defined functions in PHP will already throw # Reclassified engine warnings RFCLots of errors that previously only triggered warnings or notices, have been converted to proper errors. The following warnings were changed.
# The @ operator no longer silences fatal errorsIt's possible that this change might reveal errors that again were hidden before PHP 8. Make sure to set # Default error reporting levelIt's
now # Default PDO error mode RFCFrom the RFC: The current default error mode for PDO is silent. This means that when an SQL error occurs, no errors or warnings may be emitted and no exceptions thrown unless the developer implements their own explicit error handling. This RFC changes the default error will change to # Concatenation precedence RFCWhile already deprecated in PHP 7.4, this change is now taken into effect. If you'd write something like this:
PHP would previously interpret it like this:
PHP 8 will make it so that it's interpreted like this:
# Stricter type checks for arithmetic and bitwise operators RFCBefore PHP 8, it was possible to apply arithmetic or bitwise operators on arrays, resources or objects. This isn't possible anymore, and will throw a
# Namespaced names being a single token RFCPHP used to interpret each part of a namespace (separated by a backslash # Saner numeric strings RFCPHP's type system tries to do a lot of smart things when it encounters numbers in strings. This RFC makes that behaviour more consistent and clear. # Saner string to number comparisons RFCThis RFC fixes the very strange case in PHP where # Reflection changesA few reflection methods have been deprecated:
You should now use
If the type is a single type,
If the type is a union type however,
you'll get an instance of
Checking whether a type is a union or not can be done with an
Next up, three method signatures of reflection classes have been changed:
Have now become:
The upgrading guide specifies that if you extend these classes, and still want to support both PHP 7 and PHP 8, the following signatures are allowed:
# Stable sorting RFCBefore PHP 8, sorting algorithms were unstable. This means that the order of equal elements wasn't guaranteed. PHP 8 changes the behaviour of all sorting functions to stable sorting. # Fatal error for incompatible method signatures RFCFrom the RFC: Inheritance errors due to incompatible method signatures currently either throw a fatal error or a warning depending on the cause of the error and the inheritance hierarchy. # Other deprecations and changesDuring the PHP 7.* development, several deprecations were added that are now finalised in PHP 8.
Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter: FootnotesWhat's new in PHP 8.2 What's new in PHP 8.1 Upgrading to PHP 8 using Homebrew on a Mac The nullsafe operator The match operator in PHP 8 Named arguments in PHP 8 Property promotion in PHP 8 The latest PHP version — how modern PHP versions are managed Attributes in PHP 8 — A close look at attributes, also known as annotations PHP in 2020 What's new in PHP 7.4 What's new in PHP 7.3 What are new features in PHP 8?PHP 8 Improvements and New Features. Constructor Property Promotion.. Validation for Abstract Trait Methods.. Incompatible Method Signatures.. Arrays Starting With a Negative Index.. Union Types 2.0.. Consistent Type Errors for Internal Functions.. throw Expression.. Weak Maps.. Is PHP 8.0 good?It is one of the world's most popular programming language, used by web developers around the globe. With PHP 8, there are a number of new features that make it a great fit for large-scale enterprise applications, including web services and micro-services, and applications that run on the cloud.
What is the difference between PHP 8 and 7?PHP 8 brings a host of new features improvements, functions, and deprecations to the language compared to PHP 7. Among all of these new features, the JIT compiler is the one sharing the limelight.
What is new in PHP latest version?Latest Versions Come With More Features
One, in particular, highlights the example of not only a new feature, but also productivity. Constructor property promotion is new in PHP 8.0 and offers developers an opportunity to move quicker when defining the constructor properties of an object.
|