Oh Dear! monitors your entire website, not just the homepage. You'll get a notification as soon as your website is down, a monthly uptime report, a warning a few days before your SSL certificate expires and much more! Start your free 10 day trial now!

On using arrow functions in PHP 7.4

Original – by Freek Van der Herten – 3 minute read

In PHP 7.4 a widely requested feature landed: arrow function. In this blogpost I'd like to show you how I like to use them.

Let's start with an example from the Oh Dear codebase I tweeted out a few days ago.

In Laravel 6 and PHP 7.3 you could do this:

public static function boot()
{
    static::boot();

    static::creating(
        function (Team $team) {
            return $team->webhook_signing_secret = Str::random(32);
        }
    );
}

In Laravel 7 and PHP 7.4 you can rewrite it to this:

public static function booted()
{
   static::creating(fn (Team $team) => $team->webhook_signing_secret = Str::random(32));
}

In the code snippet above, a short closure is used. It's a bit beyond the scope of this post, but in Laravel 7, the booted lifecycle method was introduced, which will, unsuprisingly, be called we the model has booted. Using that method, you can lose the static::boot(); call from the initial code snippet.

A lot of people seemed to like it. Some didn't, and that's ok. When introducing new syntax, I don't think there's a single thing where all programmars will agree on. A comment I agree on however, is the line length. Luckily this can be easily fixed by moving the closure itself to it's own line.

public static function booted()
{
   static::creating(
      fn (Team $team) => $team->webhook_signing_secret = Str::random(32)
   );
}

To make the line length shorter, you could opt to remove the typehint. You could even rename the $team variable to $t, this is pretty common practice in the JavaScript. Personally I love typehints and full variable names, so personally I stick to the code snippet above.

I think it would be silly to blindly switch to short closures wherever it is technically possible. The question you should keep in mind when converting a closure to a short one is: "Does this make the code more readable?". Of course, the answer is subjective. When in doubt, I recommend just asking your team members, they're the ones that will have to read your code too.

If you use PhpStorm you can easily switch between a normal and a short closurse, so you can quickly get a feel of how it reads.

convert.

Let's end by taking a look at an example where I think short closures really shine: in collection chains (thanks for the example, Liam).

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

You can comment on this post by replying to this tweet.
Felix Huber liked on 11th March 2020
Daniel Schmitz liked on 11th March 2020
Kieran Holroyd liked on 11th March 2020
Joiner Leal liked on 10th March 2020
Neil Carlo Faisan Sucuangco liked on 10th March 2020
eminiarts liked on 10th March 2020
Laimonas Narbutas liked on 10th March 2020
Marc Hampson liked on 10th March 2020
Laimonas Narbutas replied on 10th March 2020
After jumping from Vue.js projects and Laravel into Angular with TS and ngrx with Symfony I have very mixed feelings about the shortcuts and all the magic. Code readability is crying in the corner...
Travis Elkins liked on 10th March 2020
Babul A. Mukherjee liked on 10th March 2020
qrazi retweeted on 10th March 2020
Dyos Deangey πŸ“Ά liked on 10th March 2020
Dyos Deangey πŸ“Ά retweeted on 10th March 2020
PHP Synopsis retweeted on 10th March 2020
togo liked on 10th March 2020
Kevin Guo liked on 10th March 2020
Corben Dallas liked on 10th March 2020
Peter Brinck 🀘 liked on 10th March 2020
Really liked the post, though!
Freek Van der Herten replied on 10th March 2020
Fixed, thanks!
SyntaxSeed (Sherri W) retweeted on 10th March 2020
DIABATE Saliou retweeted on 10th March 2020
Bert Van de Casteele retweeted on 10th March 2020
SyntaxSeed (Sherri W) liked on 10th March 2020
Mike liked on 10th March 2020
Jorge GonzΓ‘lez liked on 10th March 2020
Ryan Chandler liked on 10th March 2020
Something is missing after 2nd snnipet -- "In the code snippet above..."