PHP 7.4 in 7 code blocks

Written on 2020-05-23

PHP 7.4, the last edition in the 7.* series, brings lots of new and handy changes. This post lists the highlights, though there's much more to this release. You can read all about the full release in this post about what's new in PHP 7.4.


array_map(
    fn(User $user) => $user->id,
    $users
);

Arrow functions, a.k.a. short closures. You can read about them in depth in this post.


class A
{
    public string $name;
    
    public ?Foo $foo;
}

Type properties. There's quite a lot to tell about them.


$data['date'] ??= new DateTime();

The null coalescing assignment operator. If you're unfamiliar with the null coalescing operator, you can read all about shorthand operators in this blog.


class ParentType {}
class ChildType extends ParentType {}

class A
{
    public function covariantReturnTypes(): ParentType
    { /* … */ }
}

class B extends A
{
    public function covariantReturnTypes(): ChildType
    { /* … */ }
}

Improved type variance. If you're not sure what that's about, you should take a look at this post about Liskov and type safety.


$result = [...$arrayA, ...$arrayB];

The array spread operator. There are a few sidenotes to be made about them.


$formattedNumber = 107_925_284.88;

The numeric literal separator, which is only a visual aid.


[preloading]
opcache.preload=/path/to/project/preload.php

Preloading improves PHP performance across requests. It's a complicated topic, but I wrote about it here.

Join over 14k subscribers on my mailing list: I write about PHP, programming, and keep you up to date about what's happening on this blog. You can subscribe by sending an email to brendt@stitcher.io.

Things I wish I knew when I started programming

Things I wish I knew when I started programming cover image

This is my newest book aimed at programmers of any skill level. This book isn't about patterns, principles, or best practices; there's actually barely any code in it. It's about the many things I've learned along the way being a professional programmer, and about the many, many mistakes I made along that way as well. It's what I wish someone would have told me years ago, and I hope it might inspire you.

Read more

Comments

Loading…
No comments yet, be the first!
Noticed a tpyo? You can submit a PR to fix it.
HomeRSSNewsletterDiscord© 2025 stitcher.io