You’ve probably heard whispers of the new exciting thing in the world of PHP development, PHP 8 – the upcoming major version of PHP. Earlier this year, we published a post on the latest trends in PHP web development for 2020, and we briefly introduced PHP 8 in it. In this post, we’re going to dive deeper and take a look at what this major release has to offer.
The release date for PHP 8 is November 26, 2020, and the new version will bring a set of powerful features along with some significant improvements. Here are some of the new features offered by PHP 8:
New Features in PHP 8: An Overview
1. Union types
Union types can be quite useful in many cases, considering PHP’s dynamically typed nature. Union types are a combination of two or more types showing that any one of them can be used.
public function foo(Foo|Bar $input): int|float;
It should be noted that void can’t be a part of union type as it means “no return value at all.” Moreover, you can write nullable unions using |null or the existing ? notation.
public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;
2. JIT: The Just in Time Compiler
The JIT (Just in Time) compiler is likely to offer noteworthy performance improvements, although the context is not always of web requests.
Even if opcodes are in intermediate representation of low-level, they still need compilation into machine code. JIT does not offer any added Intermediate Representation (IR) form; however, it uses Dynamic Assembler for code generation engines – DynASM – for generating native code from PHP byte-code directly.
In a nutshell, JIT translates the intermediate code’s hot parts into machine code. It bypasses compilation and is likely to offer significant improvements in memory usage and performance.
JIT for Web Apps
As per JIT RFC, implementing the just in time compiler should improve the performance of PHP. However, when it comes to real-life applications like WordPress, would there be such improvements? According to the early tests, JIT would lead to significantly faster running of CPU-intensive workloads.
However, RFC warns that like the earlier attempts, it doesn’t currently seem to bring any considerable improvements in real-life applications like WordPress.
As per plans, more effort is to be provided to improve JIT for real-life applications with speculative optimizations and profiling.
When JIT is enabled, the CPU itself would run the code, rather than the Zend VM. This would improve calculation speed.
Web applications like WordPress depend on other factors also like database optimization, TTFB, HTTP requests, etc. Hence, for WordPress and similar applications, a great increase in PHP execution speed shouldn’t be expected. However, JIT could offer several advantages to developers.
3. Attributes
Commonly called annotations in other languages, attributes allow you to add meta data to the classes, without the need to parse docblocks.
Example of how PHP 8 attributes appear, from the RFC:
use App\Attributes\ExampleAttribute;
#[ExampleAttribute]
class Foo
{
#[ExampleAttribute]
public const FOO = ‘foo’;
#[ExampleAttribute]
public $x;
#[ExampleAttribute]
public function foo(#[ExampleAttribute] $bar) { }
}
#[Attribute]
class ExampleAttribute
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}
It should be noted that this base Attribute, called PhpAttribute in the original RFC, was later changed with another RFC.
4. Constructor Property Promotion
Constructor Property Promotion RFC introduces a new syntax that’s more concise and simplifies the declaration of properties, making it less redundant and shorter. This relates to promoted parameters, i.e., the method parameters that are prefixed with protected, public, and private visibility keywords.
Currently, all properties need to be repeated many times before they can be used with objects. This RFC aims to combine the constructor and parameter definition. Hence, PHP 8 offers a more usable way to declare parameters.
Instead of writing class properties along with a constructor for them, they can now be combined into one by PHP.
Instead of this:
class Money
{
public Currency $currency;
public int $amount;
public function __construct(
Currency $currency,
int $amount,
) {
$this->currency = $currency;
$this->amount = $amount;
}
}
It can now be this:
class Money
{
public function __construct(
public Currency $currency,
public int $amount,
) {}
}
5. Named arguments
With Named arguments, you can pass in values to a function by mentioning the value name, ruling out the need to consider their order. You can skip the optional parameters too.
function foo(string $a, string $b, ?string $c = null, ?string $d = null)
{ /* … */ }
foo(
b: ‘value b’,
a: ‘value a’,
d: ‘value d’,
);
6. Allowing ::class syntax on objects
This is a small yet handy new feature. Now you can use ::class on objects, instead of the need to use get_class () on them. Its functionality is the same as that of get_class ().
$foo = new Foo();
var_dump($foo::class);
7. Trailing comma in parameter list
Commas appended to item lists in different contexts are referred to as trailing commas. Trailing commas were introduced by PHP 7.2 in list syntax while they were introduced by PHP 7.3 in function calls.
PHP 8 will introduce trailing commas in parameter lists with methods, functions, and closures.
Example:
public function(
string $parameterA,
int $parameterB,
Foo $objectfoo,
) {
// …
}
Here, the comma highlighted in green is the trailing comma. The use list of closures also supports trailing commas.
8. Abstract methods in traits improvements
Traits can outline abstract methods that must be implemented by classes that use them. However, there’s a caution – before PHP 8, these method implementations’ signatures were not validated. What was valid is as follows:
trait Test {
abstract public function test(int $input): int;
}
class UsesTrait
{
use Test;
public function test($input)
{
return $input;
}
}
With PHP 8, proper validation of method signatures will be conducted when a trait is used, and its abstract methods are implemented. So, the following needs to be written instead:
class UsesTrait
{
use Test;
public function test(int $input): int
{
return $input;
}
}
With all these and many more improvements and new features in PHP 8, this latest version of PHP is set to take PHP development to the next level. We’re eagerly waiting for its launch and thinking about how we can take advantage of PHP 8 performance and what breakthroughs we can incorporate in our PHP development methods with the help of new PHP 8 features.
Have a PHP project in mind? We are equipped with vast levels of experience and technical expertise to provide you with the best. Let’s talk today!