
PHP in 2022
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:
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 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!
# 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.
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!