DEV Community

Dan Silcox
Dan Silcox

Posted on

PHP 8 Released! What are your favourite features?

PHP 8.0 has finally been released and as a long-time PHP developer (among other things) I'm excited to see PHP maturing and getting some really great improvements. It gets a lot of stick and like any language it has its limitations and annoyances, but it's great for some things and these just make it far better than PHP 7.x (which was already 100x better than PHP 5.x!).

What are your favourite features to the new version? Will you be migrating your production PHP apps to PHP 8.0 straight away or hold off for a bit?

Here are a few of my favourites, in no particular order:

Constructor property promotion

Languages like TypeScript already have this and developers can save loads of time when writing value objects, entities, DTOs, etc:

PHP 7.3

<?php 
class MyClass {
    /** @var string */
    private $thing;
    /** @var int */
    private $otherThing;

    /**
     * @param string $thing
     * @param int $otherThing
     */
    public function __construct(
        string $thing,
        int $otherThing
    ) {
        $this->thing = $thing;
        $this->otherThing = $otherThing;
    }

Enter fullscreen mode Exit fullscreen mode

PHP 8.0

<?php

class MyClass {
    public function __construct(
        public string $thing,
        public int $otherThing
    ) { 
    }
}

Enter fullscreen mode Exit fullscreen mode

Attributes

At last, we can get away from comments that can break applications!

I realise that syntactically they don't look that different to annotations, but attributes are an actual language feature, not a reflection hack. In C# you can use attributes for everything from input filter validation to Open API definitions and I can see much the same thing happening in the PHP ecosystem over time. It just relies on the major frameworks adopting these rather than the abominations of annotations!

The JIT

This will not make a lot of difference for many web apps, blogs, etc, but for backend long-running scripts, CPU intensive operations, etc, this will be a game changer. The graph on the PHP 8 release page shows some examples of how much difference it can make:

PHP 8 JIT benchmarking

There are so many more features I've not mentioned simply because of time. I'd love to see in the comments what everyone else is looking forward to and also whether you've already been using the beta/RC versions or are going to upgrade to PHP 8 ASAP.

Top comments (1)

Collapse
 
dakujem profile image
Andrej Rypo

Developers should not forget attributes are still only meta data, even though part of the language itself. They allow for ditching annotation parsing and bring much better performance, though, for which I'm happy.

I personally welcome most the small syntactic changes that remove subtle annoyances, like throwing inside an expression, $var::class, better variadic parameters and better rype variance in inheritance, nullsafe operator for functions,

... and union types. Finally!