from our blog

PHP 8 Features and How These Affect WordPress

Posted by Simranjeet Singh

Website Development, web development, Development

PHP 8 - It's New, It's Better, It's Futuristic

Brace yourself; PHP 8 is launching at the end of November or the starting of December. Inevitable excitement as it contains loads of features and performance improvements. While this also means that developers need to make changes in their codes to make them compatible with the new version. If you have a habit of timely updating your code, then you already know the drill.

Among all the other new features and improvements, JIT Compiler is the hot topic. So, let's begin with that.

JIT Compiler – Just-In-Time Compiler

As in the name, in JIT, a program is compiled in the machine code just before it is implemented. This feature introduces a significant performance improvement, but the real question is; would it make much of a difference in live web apps such as WordPress?

- JIT-enabled code would run by the CPU itself, which will definitely improve calculation speed. However, live apps like WordPress are dependent on Database Optimization, TTFB, HTTP Requests, and other factors. So, we can't say that live web apps such as WordPress will show a significant improvement in speed and performance.

Dmitry Stogov of PHP RFC wrote, "we can't further improve the performance of PHP unless we use JIT. Using JIT may open the door for PHP being more frequently used in other, non-Web, CPU-intensive scenarios - where the performance benefits will actually be very substantial - and for which PHP is probably not even being considered today. Making JIT available can provide us (with additional efforts) with the ability to develop built-in functions in PHP, instead of (or in addition to) C - without suffering the huge performance penalty that would be associated with such a strategy in today's, non-JITted engine."

Read about PHP RFC: JIT in detail.

Union Types 2.0

PHP and Types have a long-lasting relation. We have Scalar Types, Nullable Types, Return Types, and Property Types. Now, PHP 8 has introduced another one, Union Types.

Understanding Union Types: Till now, you could declare only one Type for arguments, parameters, properties, and others. Now you can declare multiple Types, i.e., more than one Type for properties, return types, arguments, parameters, and so on.

This allows the developers to declare more information from phpdoc into function signatures.

Nikita Popov highlights the benefits of Union Types 2.0

  • Types are actually enforced, so mistakes can be caught early.
  • Because they are enforced, type information is less likely to become outdated or miss edge-cases.
  • Types are checked during inheritance, enforcing the Liskov Substitution Principle.
  • Types are available through Reflection.
  • The syntax is a lot less boilerplate-y than phpdoc.

Official PHP RFC: Union Types 2.0 in full detail

Attributes

It is not the first time that PHP has announced Attributes. Efforts were made in the previous versions as well to launch the features of attributes in PHP, but those were not quite successful. We are hoping for a positive outcome of this feature this time. Attributes are used to add meta data to classes. Offering the same functions, they have different names for other languages. Annotations offer similar functions in Java, and Attributes are in C# as well, C++. They are known as decorators in JavaScript and Python.

Examples

Functions

<<attribute('foo')>>

function example(){}

Classes

<<attribute('foo')>>

class Example {}

Function/Method Arguments

function example(<<attribute('foo')>> string $foo) {}

Class Properties

class Foo {

<<attribute('foo')>>

private string $foo;

}

Class Constants

class Foo {

<<attribute('foo')>>

private const FOO = 'foo';

}

Closures

$fn = <<attribute('foo')>> fn() => 1 > 2;

$fn = <<attribute('foo')>> function() {

return 1 > 2;

}

Anonymous Classes

$instance = new <<attribute('foo')>> class {};

PHP RFC: Attributes V2

The Nullsafe Operator

Previously, the null coalescing operator didn't work on method calls. You needed intermediate checks or optional helpers from third-party frameworks. But, with the new Nullsafe Operator, the developers can have a null coalescing-like performance on methods. Isn't it cool?

Full Official statement on PHP RFC: Nullsafe Operatorby Ilija Tovilo

Named Arguments

You can also call it named parameters because that's what it does. Named arguments to allow the passing of values to a function based on the name of the parameter and not the position of the parameter.

You can know about these in detail on PHP RFC: Named Arguments

Match Expression

If you know about Switch Expression, then consider Match Expression a bigger and better version of it.

Match Expression can…

Returns values, so you have to assign value only once

  • It can combine conditions
  • It doesn't necessarily require 'break' values
  • It doesn't do any type coercion
  • It uses strict type comparisons

Constructor Property Promotion

CPP – Construction Property Promotion has introduced a more to the point syntax to simplify the property declaration. In PHP 8, now the developers have a better and usable way of declaring parameters because it reduces the redundancy and makes it shorter.

PHP RFC: Constructor Property Promotion

Inheritance with Private Methods

To date, PHP applied the same inheritance checks in private, protected, and public methods. This didn't make sense because the child classes don't access the private methods. The private methods cannot be fetched outside the scope they are defined in, so the rules should not be applied.

PHP RFC: Remove Inappropriate Inheritance Signature Checks on Private Methods

Weak Maps

A weak map is a collection of objects where keys are weakly referenced. Weakly referenced means that they are not prevented from being destroyed. That's why WeakMap class is introduced. It will be used to create objects as weak map keys so that they can be destroyed. This will ultimately improve performance and also reduce memory leaks. PHP will collect these objects are garbage if there's nothing else referencing them.

Here's PHP RFC: Weak Maps by Nikita Popov

Class on Objects

Previously, the developers used get_class() on the objects, now they can simply use ::class on them. It's not a major input but will ease the coding for developers in the long run once they get used to it.

Non-Capturing Catches

With PHP 8, you can ditch the variable for catching an exception. For this, you are required to specify the Type of catch. You cannot leave it empty. In case you want to catch all errors and exceptions, you can use 'Throwable' as the catching Type.

Other New Features and Improvements Include

  • New Static Return Type
  • New Mixed Type
  • Throw Expression
  • Trailing Comma in Parameter Lists
  • Create DateTime Objects from Interface
  • New Stringable Interface
  • New str_contains() function
  • New str_starts_with() and str_ends_with() functions
  • Abstract methods in traits improvements
  • Object implementation of token_get_all()
  • Variable syntax tweaks
  • Type annotations for internal functions
  • Default PDO error mode

PHP 8 and WordPress 5.6

As mentioned earlier, WordPress relies on Database Optimization, TTFB, HTTP Requests and other factors, so would simply updating PHP impact it much or not?

WordPress 5.5 update made the file manager vulnerable, and it was launched without the jQuery Migrate – a JavaScript library, which disrupted the functioning of many plugins.

Now, we have PHP 8 coming soon along with WordPress 5.6 just a couple of weeks later of PHP 8. So, a lot of disruption is expected. Developers need to be on their toes for the next months to see everything on their website is running smoothly. Some of the plugins might be broken or won't work for any other reason. Just keep your eyes and ears open.

Keeping the software updated is the best practice, but if something happens as it did in the case of the WordPress 5.5 update, the whole WordPress community needs to come together to find the solution.

Simranjeet Singh

Written by Simranjeet Singh

Writer | HubSpot Certified Inbound Marketer and Content Marketer