Need to upgrade to the latest version of Laravel? Get instant, automated Laravel upgrades with Laravel Shift

PHP in 2022

It's the fourth time I'm writing a yearly "PHP in 20XX" post, and I must admit I've never been as excited about it as this year: we've seen awesome new features added to PHP, with stuff like attributes, enums, promoted properties and fibers; and on top of that, the static analysis community is making great progress, my personal favourite feature is PhpStorm now supporting generics when writing code.

Exciting times are ahead, let's take a look at modern day PHP!


# PHP 8.1

I can't help but start this list with the newest PHP version, released just a little more than one month ago. My main projects are already being prepared to run on PHP 8.1 in production, which I must admit I'm very excited about. You might not expect it from a minor release — there are no major breaking changes and only deprecation notices added — but PHP 8.1 brings some very cool features. here's my personal top three.

Enums are now built-into the language:

enum Status
{
    case draft;
    case published;
    case archived;
    
    public function color(): string
    {
        return match($this) 
        {
            Status::draft => 'grey',   
            Status::published => 'green',   
            Status::archived => 'red',   
        };
    }
}

We're able to use new in initializers:

class PostStateMachine
{
    public function __construct(
        private State $state = new Draft(),
    ) {
    }
}

And, of course, readonly properties:

class PostData
{
    public function __construct(
        public readonly string $title,
        public readonly PostState $state,
        public readonly DateTimeImmutable $publishedAt,
    ) {}
}

Which, combined with PHP 8.0's promoted properties, make for some very clean data classes. Just to visualise the difference, here's that same class, with the same functionality, written in PHP 5.6:

class PostData
{
    /** @var string */
    private $title;
    
    /** @var State */
    private $state;
    
    /** @var \DateTimeImmutable|null */
    private $publishedAt;
   
   /**
    * @param string $title 
    * @param State $state 
    * @param \DateTimeImmutable|null $publishedAt 
    */
    public function __construct(
        $title,
        $state,
        $publishedAt = null
    ) {
        $this->title = $title;
        $this->state = $state;
        $this->publishedAt = $publishedAt;
    }
    
    /**
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;    
    }
    
    /**
     * @return State 
     */
    public function getState() 
    {
        return $this->state;    
    }
    
    /**
     * @return \DateTimeImmutable|null 
     */
    public function getPublishedAt() 
    {
        return $this->publishedAt;    
    }
}

Can you see why I'm excited? PHP is getting these awesome syntax improvements with every release. It's only getting more and more fun to write it! Of course, there are a lot more features added in modern PHP, check out my 3-minute video if you want a quick rundown, or you can scroll to keep reading — there's much more exciting about PHP in 2022!

# Static Analysis

I've already briefly mentioned it: static analysis in PHP is growing significantly.

If you just want a quick read about why static analysis matters in PHP, and why I'm so excited about it, you could check out this blog post: "We don't need runtime type checks".

# The PHP foundation

Two months ago, the PHP world got some pretty big news, maybe even the biggest news of 2021: Nikita, one of most active core maintainers is stepping down to work on LLVM, but at the same time there's also a new initiative backed by several big companies to finally make core development sustainable.

In short, there's the PHP Foundation, a non-profit with the only goal to fund PHP core development. The initiative is driven by JetBrains, who have already pledged $100,000 to the project. Alongside many others, they have now raised $329,920.75; a good start!

That money is used to fund core development, and opens doors for people to work on PHP who were previously unable. You can read more about the Foundation's mission and goals in JetBrains' blog post.

# The ecosystem

Just like every year, I can't go without mentioning Packagist, now with over 3 million registered versions and more than 300.000 packages. As you can see, the ecosystem just keeps growing and growing, 2022 won't be any different.

Oh, by the way, just recently Packagist passed the milestone of having handled over more than 50 billion installs. Congrats Packagist!

# Async PHP

One exciting development in the async community, is that the developers from both Amp and ReactPHP — the two major async players — have come together to make a fiber-compatible event loop implementation called Revolt PHP.

In comparison to the community at large, async PHP is only used by a small portion of it; but nevertheless it's still good to see the async community is going strong and embracing modern PHP.

# Serverless PHP

An interesting development I've been following along the sideline, is serverless PHP. My buddy Matthieu Napoli is making it his mission to educate PHP developers about this relatively new way of using PHP, and he seems to be doing pretty well. You can check out Bref, his open source project to make serverless PHP easy, or check out his course about serverless PHP in 2022.

I haven't had any use cases for it myself, but I know more and more people running serverless PHP in production, so it's definitely something worth keeping an eye on.

Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can subscribe to my mailing list: send an email to brendt@stitcher.io, and I'll add you to the list.

# Looking forward

So what about this year? I'm looking forward to see where static analysis is going, and am especially curious about even better integration with PhpStorm. I find that realtime static analysis — static analysis in your code while you're writing it — offers much more value during the development phase itself.

I'm a little worried now that Nikita has stepped down. He's definitely not the only person capable of working on PHP's core, but he did a tremendous amount of work these past years with PHP 8.0 and 8.1. I hope the PHP Foundation will be up to speed soon and that there are enough core developers who have time to work on PHP next year. PHP 8.2 is already in development, although there aren't many RFCs drafted yet.

I don't think 2022 will be the most mind blowing year for PHP, but rather a year of adding stability. Nothing wrong with that.


These were the things that stood out the most to me personally in 2021, and that make me excited for PHP in 2022: awesome new syntax, stability for core development thanks to the PHP Foundation, static analysis is growing stronger and better, and lots of interesting developments across the community.

What are you most excited about? Let me know on Twitter or via email; I'd love to hear your thoughts!