LifestylePHP in 2022

PHP in 2022

-

- Advertisment -

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you
can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

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 is 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:

- Advertisement -
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,
    ) {}
}
- Advertisement -

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 BlogData
{
    
    private $title;
    
    
    private $state;
    
    
    private $publishedAt;
   
   
    public function __construct(
        $title,
        $state,
        $publishedAt = null
    ) {
        $this->title = $title;
        $this->state = $state;
        $this->publishedAt = $publishedAt;
    }
    
    
    public function getTitle()
    {
        return $this->title;    
    }
    
    
    public function getState() 
    {
        return $this->state;    
    }
    
    
    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!

YouTube video

# Static Analysis

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

  • Frameworks like Laravel are embracing static typing more and more (writing code and docblocks with the sole purpose of helping static analysis);
  • PhpStorm added support for generic types, it’s a pretty big deal if you can write generic code and have your IDE understand it while you’re writing it;
  • PhpStan and psalm are only growing; and finally
  • my own newsletter series about static analysis has been doing pretty well with over 1500 participants, more and more people are getting interested in the topic.
- Advertisement -

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“.

Do you want to learn more about PHP 8.1? There’s The Road to PHP 8.1.
For the next 10 days, you’ll receive a daily email covering a new and exiting feature of PHP 8.1; afterwards you’ll be automatically unsubscribed, so no spam or followup.
Subscribe now!

# 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 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 follow me
on Twitter or subscribe to my newsletter:

These are 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!

NOW WITH OVER +8500 USERS. people can Join Knowasiak for free. Sign up on Knowasiak.com
Read More

- Advertisement -

2 Comments

You might also likeRELATED
Recommended to you

AtoB (YC S20) is building Stripe for Transportation and hiring early crew contributors

The trucking and logistics industry provides the backbone of the economy. But the payments infrastructure on which it runs is broken. For the hard-working men and women of this sector, the existing suite of payment tools is outdated, difficult to use, prone to fraud, and saddled with shady fee structures. The incumbent players in this…

Is It Time for the JavaScript Temporal API?

Date handling in JavaScript is ugly. The Date() object has not changed since the first Java-inspired implementation in 1995. Java scrapped it but Date() remained in JavaScript for backward browser compatibility.Issues with the Date() API include:it’s inelegantit only supports UTC and the user’s PC timeit doesn’t support calendars other than Gregorianstring to date parsing is…

Write Shitty Code

I know how to write good code.Usually it means spending more time now to make the code better for...
- Advertisement -

Billionaire Chamath Palihapitiya: ‘nobody cares’ about China’s Uyghur genocide

WASHINGTON – Billionaire investor Chamath Palihapitiya triggered a backlash on social media after saying during a recent episode of his podcast that "nobody cares" about the ongoing human rights abuses against the Uyghurs in China.During a 90-minute episode, Palihapitiya told co-host Jason Calacanis on their "All-In" podcast that he would be lying if he said that…

Show HN: Declarative Instrumentation for Python

Pyccolo is a library for declarative instrumentation in Python; i.e., it lets you specify the what of the instrumentation you wish to perform, and takes care of the how for you. It aims to be ergonomic, composable, and portable, by providing an intuitive interface, making it easy to layer multiple levels of instrumentation, and allowing…

Must read

Gitpod Open-Source Sustainability Fund

The myriad of tools relied upon by developers every...

Research: Restricting Airbnb Rentals Reduces Development

Much has been written about the harm caused by...
- Advertisement -