PHP 8 is coming and WordPress runs on PHP. It’s the core to absolutely everything the platform does, from its underlying functions to add ons like themes and plugins. Out of all the languages WordPress uses, PHP is by far the most important.
As a consequence, you can imagine that major PHP updates are a big deal. From speeding up your website to adding new attributes and functions for developers to work with, it’s important to keep up with it all. Even if you’re not a developer yourself, you should have a general idea what to look out for when it’s time to upgrade your PHP version.
Let’s go over it now: the major new features in PHP 8, why you need to upgrade as soon as possible, and how to do it once official support rolls out.
Why Pay Attention to PHP Updates?
With every PHP update comes a whole host of major new features. Not all of them are relevant to casual users, and some are even too niche for a majority of developers. But aspects like increased speed, breaking changes that could damage existing code, and other relatively minor addons could still have an impact on your development workflow.
PHP as an engine is constantly changing, with new bits being added or modified to optimize the code and make it easier to work with. Theme and plugin creators as well as website developers will want to stay up to date with what’s going on.
That way, you won’t have to slog through lots of documentation to get caught up several updates from now.
While each new version brings mostly positive changes, sometimes these tweaks can cause issues with existing code. Outdated attributes and backwards incompatible code could end up breaking your site and causing parts of it to stop functioning.
You’ll want to know what these breaking changes are so you or your developers can address them in your custom code, and also make use of new, more optimized expressions.
And even if you’ve never touched a piece of custom PHP code in your life, when WordPress adds support for the next PHP version, you’ll be faced with a choice: Upgrade or don’t?
The answer is almost always yes, but you’ll want to at least skim over the major features and make sure there’s nothing in there that could cause problems for you.
What’s New in PHP 8?
While it’s not the biggest update by far, there are some pretty exciting changes in PHP 8. The smaller update is also a blessing, as it means there’s fewer breaking changes to cause issues with existing code. You get all these new features without having to spend hours on troubleshooting.
This is far from all the extras coming with PHP 8, so make sure to check out the full changelog for more.
PHP 8.0 releases on November 26, 2020, so keep an eye out for that and get ready for eventual WordPress support.
JIT (Just In Time) Compiler
Probably the biggest and most exciting addition of PHP 8 is the JIT compiler. PHP is an interpreted language, which means it runs in real time, rather than being compiled and run at launch. JIT brings compiled code to PHP, and with it, better performance — in some situations.
If you’re working with web apps as most PHP developers are, JIT won’t help much as these performance benchmarks show. But when it comes to certain tasks like 3D rendering, data analysis, artificial intelligence, and other long-running processes, it makes a huge difference.
These aren’t the most common applications of PHP, but plenty of developers are branching out, so this makes the engine much more flexible. JIT is sure to open PHP’s horizons and bring in devs interested in trying out new things.
But for existing projects, it might not do much. Before implementing JIT, make sure to test it out in an isolated environment and see whether it improves your performance or not.
Attributes
PHP now supports attributes, or small pieces of metadata you add to parts of your code: functions, classes, parameters, that kind of thing. That means you no longer have to use docblocks as a workaround! It’s a small change, but one that makes life so much easier.
You can add multiple attributes to any piece of code, import them with use statements, and add parameters to attributes as well. Add an attribute to your code with two less than and two greater than signs: <<Attribute>>
.
One thing to note is that attributes are not backwards compatible and will cause errors if ported into older versions of PHP.
The docblock workaround is functional, but it’s always been a bit clunky. Now you can add attributes directly. For such a small addition, it has huge implications.
Match Expression
If switch statements are a more powerful version of if statements, the new match expression improves on that even more. It’s shorter, stricter (so it catches bugs better), combines conditions, and doesn’t require a break statement. In many ways, it’s just better!
The one thing switch has over match right now: it can use multiple expressions. Other than that, you should read up on match expressions because they’re looking like a superior replacement you’ll want to make use of.
Union Types
Types are a key part of PHP, assigning a variable as an integer, boolean, null, and so on. But before you could only assign a variable with a single type. Now, they can be assigned with two or more types: a union type!
For example, you can assign an integer and float type, and it can use either one of those. These are specified with a line between each type, for example: int|float
.
The one thing you can’t do is combine void
and another type. These are mutually exclusive. And duplicate or redundant types (such as int|int
) are also not allowed.
You could previously use PHPDoc annotations to make something like a functional union type, but now you can skip the unwieldy workarounds and just assign variables with multiple types.
Static Return and Mixed Types
Here’s a small one: Returns in PHP are used to return the value of a function. But before, using the static type wasn’t an option. self
and parent
were valid return types, but not static
. Thanks to PHP 8, this issue has been addressed. It’s not a common use case, but when it does come up, you’ll be glad to have it.
There’s also the new mixed type, or more accurately pseudo-type since it represents all the other types that exist in PHP. You use it when dealing with a situation where you need to work with several different types. Mixed is now also the assumed default when you don’t specify a type.
Oftentimes it’s better to use the new union type instead of mixed as you’ll likely want to be more specific, but this one could still come in handy.
Neither of these are quite as big as the union type, but they make a difference.
New Functions
PHP 8 comes with several new functions to work with.
str_starts_with and str_ends_with
— Checks if a string starts or ends with a certain line.str_contains
— Similar usage, but checks an entire string.fdiv
— Allows division that consistently reacts when dividing by zero.get_resource_id
— Now it’s easy to get the ID of a resource like a database connection.get_debug_type
— Acts very similar togettype
, but returns more information such as class name.preg_last_error_msg
— When working with old PHP functions that require you to manually check for errors, this returns a human-readable error message.
Named Arguments
Some love it, some hate it, but named arguments are now in PHP 8. Arguments allow you to pass data to a function, but named arguments allow you to do it by passing the argument name instead of the order. You can still combine them with ordered arguments, but you get to skip optional parameters if you want to.
What this practically means is cleaner code that reduces the number of errors you encounter. The more concise your code, the better — it’s just easier to work with.
It can cause issues with backwards compatibility and changing argument names causing breaks in older versions of a program, but for many programmers named arguments are a breath of fresh air.
Breaking Changes
With PHP 8 comes several breaking changes, which could cause errors when upgrading or when porting back into older versions. Take a look at the list and see what may be relevant to you; here are a few of the big ones.
- Engine warnings in PHP have been reclassified so they show an appropriate severity level. (Fatal errors, notices, warnings, etc.) This is a minor breaking change that won’t affect most projects.
TypeError
now consistently shows on internal functions; this has a bigger impact on backwards compatibility than you might think, but it’s a good change.- Various changes were made to error reporting levels that were previously set as silent and ignored, so new errors that were before hidden to you may show up now.
- Sorting in PHP is more stable and reliable. PHP 7 already made major changes to sorting and this one is expected to do less damage to existing code.
- Namespaced names are now a single token and can no longer have whitespace inside them. The impact of this change is small and easy to fix.
- Methods with a name matching the class were interpreted as constructors. Now you need to use the
__construct()
method. - And dozens of other small changes that could rarely cause problems.
All in all, you shouldn’t have too much trouble with this update.
How to Upgrade Your WordPress PHP Version
It’s important to keep your PHP version up to date. A shockingly significant portion of WordPress users still run PHP 5.6, which is a huge liability. Even if you’re on PHP 7.4 and totally up to date, you should be prepared to update to PHP 8 when it comes out.
Running old versions of PHP is just a security risk. Outdated versions have security flaws hackers can exploit. The only way to patch those holes is to keep PHP up to date at all times.
Speed is another reason to upgrade. With every PHP update comes optimized functions and faster speeds. The biggest difference would be between 5.6 and 7.4, but you’ll definitely see a speed increase in PHP 8.
The final reason to update: compatibility. Outdated PHP can easily cause issues, breaking your websi
te or causing other glitches. Some plugins or themes require an up-to-date version of PHP to function, so you’ll want to keep it updated.
PHP 8 isn’t out quite yet, so you won’t be able to update. You’ll also need to wait for WordPress to start supporting the newest version. But if you’re sitting on an older version of PHP, such as PHP 5.6, you should consider making the jump now. There are many breaking changes in PHP 7.x and it’ll be easier to patch those up now than to make an even bigger leap straight to PHP 8.
Upgrading PHP is usually fairly simple. All it takes is logging into cPanel, or whatever admin dashboard your host is using, finding the PHP Version Manager, and ticking the latest version. You may need to check with your host to make sure they support the newest PHP update. Some run on older software. More details in the linked article.
Pay Attention to PHP 8
It may not be the monster of an update PHP 7.0-7.4 was, but PHP 8 has a lot to offer. Sorting through all that documentation and puzzling out the most important features can be difficult, so we’ve gathered it all here for you.
Though it may not be super groundbreaking, PHP 8 has several important features you’ll want to get ready for. The JIT compiler opens up new horizons for the language. On a smaller scale, the new attributes, match expression, and functions will be very useful for plenty of developers. And, of course, union types and named arguments.
Watch out for the breaking changes, but overall this update is fairly light on the major backwards-incompatible features.
Developers: What part of this update will you be using the most? Let us know what you think of PHP 8’s new features in the comments section below!
The post PHP 8: What’s New? (JIT Compiler, Attributes, and Other Features) appeared first on Torque.