New features in PHP 7.4

PHP 7.4 is due to be released at the end of this month, here's a round-up of the things we're looking forward to

Written 1 week ago on Nov 8, 2019

The next minor release of PHP, version 7.4 is due to land on the 28th November 2019. Whilst PHP 8 with JIT will be the next big change for the language, 7.4 still looks like it'll provide a few nice things for the PHP developer. We'll run through the interesting changes...

Spread Operator in Array Expression

The spread|splat operator has been around since 5.6 and essentially allows unpacking of an array into its elements. You can use it to make variadic functions but also to pass an arbitrary number of arguments to a function, it's very useful, sometimes.


$concat = function(...$args) {
    echo implode($args, ',');
}
$array = ['This', 'is', 'an', 'unrealistic', 'programming', 'situation'];

$concat(...$array);

See, very useful. PHP 7.4 is bringing this syntax to array expression, ie you can unpack arrays into other arrays, like this:


$arrayA = ['some', 'text'];
$arrayB = ['do', 'you', 'want', 'to', 'read', $arrayA];

This is a lot more performant than using array_merge as it's a language construct, not a userland function. If it supported string keys it would be a great replacement for using array_merge, but it doesn't, so it's not, but it's still good.

Arrow Functions

There's been a lot of noise about these for a while in the PHP community. Using anonymous functions in PHP at the moment is a bit grim, if you look at the following:


array_filter($paths, function ($v) use ($names) {
    return in_array($v, $names);
});

Having to write out the code for these everytime can be tedious, especially when the function is simple, which it usually will be. Also having to remember to import the variables into scope using the use keyword is a pain, and it looks a mess.

Arrow functions allow you to write something to replace the above with something that looks a lot more like JavaScript.


array_filter($paths, fn($v) => in_array($v, $names));

If that doesn't make you smile then I don't know what will, certainly not in this article anyway.

Null Coalescing Assignment Operator

The null coalescing operator ?? has been massively useful, it's led to lots less code clutter for me personally and it reads well IMO.

The null coalescing assignment operator allows a similar clutter-free way to assign variables based on their current value, so:


$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';

Becomes:


$this->request->data['comments']['user_id'] ??= 'value';

What's not to like??=

Typed Properties

With scalar type hinting and return types PHP's type system can be used to write much stricter code, it allows you to implicitly depend on what's coming back from a function call, when used with strict_types you can write code that won't give you nightmares.

The more I've used type hinting and returns it's become more and more obvious where you can't use it. The most glaring is in class properties, it just feels wrong that you can't enforce the type of properties.

This is another one that will remove lots boilerplate and make code instantly more readable, if you consider the example from the RFC the benefits are clear to see.


class User {
    /** @var int $id */
    private $id;
    /** @var string $name */
    private $name;
 
    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
 
    public function getId(): int {
        return $this->id;
    }
    public function setId(int $id): void {
        $this->id = $id;
    }
 
    public function getName(): string {
        return $this->name;
    }
    public function setName(string $name): void {
        $this->name = $name;
    }
}

Becomes


class User {
    public int $id;
    public string $name;
 
    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
}

An easy win.

Summary

So, those are the main things I'm interested in using in the upcoming release. There are quite a few other additions and some new deprecations that will take effect in PHP 8. There's an interesting sounding opcache feature for preloading php libraries into shared memory for re-use, this could be a good gain on busy systems.

All in all it's a nice step forward for PHP, it builds on PHP 7s type system but should still allow lots of very old code to still run without issues.


Article Category Tags

Click one to see related articles hand-written by Cowshed Works

PHP Web Development

Want to discuss your next web project?

We're happy to host meetings at the Cowshed, we're equally happy to come and meet you to discuss your project.

Just drop us a line and we'll get it booked in and get the ball rolling.

Cowshed Works

Testimonials